spi_fsl_spi.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. /*
  2. * Freescale SPI controller driver.
  3. *
  4. * Maintainer: Kumar Gala
  5. *
  6. * Copyright (C) 2006 Polycom, Inc.
  7. * Copyright 2010 Freescale Semiconductor, Inc.
  8. *
  9. * CPM SPI and QE buffer descriptors mode support:
  10. * Copyright (c) 2009 MontaVista Software, Inc.
  11. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/delay.h>
  23. #include <linux/irq.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/spi/spi_bitbang.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/fsl_devices.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/mm.h>
  30. #include <linux/mutex.h>
  31. #include <linux/of.h>
  32. #include <linux/of_platform.h>
  33. #include <linux/gpio.h>
  34. #include <linux/of_gpio.h>
  35. #include <sysdev/fsl_soc.h>
  36. #include <asm/cpm.h>
  37. #include <asm/qe.h>
  38. #include "spi_fsl_lib.h"
  39. /* CPM1 and CPM2 are mutually exclusive. */
  40. #ifdef CONFIG_CPM1
  41. #include <asm/cpm1.h>
  42. #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_CH_SPI, 0)
  43. #else
  44. #include <asm/cpm2.h>
  45. #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_SPI_PAGE, CPM_CR_SPI_SBLOCK, 0, 0)
  46. #endif
  47. /* SPI Controller registers */
  48. struct fsl_spi_reg {
  49. u8 res1[0x20];
  50. __be32 mode;
  51. __be32 event;
  52. __be32 mask;
  53. __be32 command;
  54. __be32 transmit;
  55. __be32 receive;
  56. };
  57. /* SPI Controller mode register definitions */
  58. #define SPMODE_LOOP (1 << 30)
  59. #define SPMODE_CI_INACTIVEHIGH (1 << 29)
  60. #define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
  61. #define SPMODE_DIV16 (1 << 27)
  62. #define SPMODE_REV (1 << 26)
  63. #define SPMODE_MS (1 << 25)
  64. #define SPMODE_ENABLE (1 << 24)
  65. #define SPMODE_LEN(x) ((x) << 20)
  66. #define SPMODE_PM(x) ((x) << 16)
  67. #define SPMODE_OP (1 << 14)
  68. #define SPMODE_CG(x) ((x) << 7)
  69. /*
  70. * Default for SPI Mode:
  71. * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
  72. */
  73. #define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
  74. SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
  75. /* SPIE register values */
  76. #define SPIE_NE 0x00000200 /* Not empty */
  77. #define SPIE_NF 0x00000100 /* Not full */
  78. /* SPIM register values */
  79. #define SPIM_NE 0x00000200 /* Not empty */
  80. #define SPIM_NF 0x00000100 /* Not full */
  81. #define SPIE_TXB 0x00000200 /* Last char is written to tx fifo */
  82. #define SPIE_RXB 0x00000100 /* Last char is written to rx buf */
  83. /* SPCOM register values */
  84. #define SPCOM_STR (1 << 23) /* Start transmit */
  85. #define SPI_PRAM_SIZE 0x100
  86. #define SPI_MRBLR ((unsigned int)PAGE_SIZE)
  87. static void *fsl_dummy_rx;
  88. static DEFINE_MUTEX(fsl_dummy_rx_lock);
  89. static int fsl_dummy_rx_refcnt;
  90. static void fsl_spi_change_mode(struct spi_device *spi)
  91. {
  92. struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
  93. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  94. struct fsl_spi_reg *reg_base = mspi->reg_base;
  95. __be32 __iomem *mode = &reg_base->mode;
  96. unsigned long flags;
  97. if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
  98. return;
  99. /* Turn off IRQs locally to minimize time that SPI is disabled. */
  100. local_irq_save(flags);
  101. /* Turn off SPI unit prior changing mode */
  102. mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
  103. /* When in CPM mode, we need to reinit tx and rx. */
  104. if (mspi->flags & SPI_CPM_MODE) {
  105. if (mspi->flags & SPI_QE) {
  106. qe_issue_cmd(QE_INIT_TX_RX, mspi->subblock,
  107. QE_CR_PROTOCOL_UNSPECIFIED, 0);
  108. } else {
  109. cpm_command(CPM_SPI_CMD, CPM_CR_INIT_TRX);
  110. if (mspi->flags & SPI_CPM1) {
  111. out_be16(&mspi->pram->rbptr,
  112. in_be16(&mspi->pram->rbase));
  113. out_be16(&mspi->pram->tbptr,
  114. in_be16(&mspi->pram->tbase));
  115. }
  116. }
  117. }
  118. mpc8xxx_spi_write_reg(mode, cs->hw_mode);
  119. local_irq_restore(flags);
  120. }
  121. static void fsl_spi_chipselect(struct spi_device *spi, int value)
  122. {
  123. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  124. struct fsl_spi_platform_data *pdata = spi->dev.parent->platform_data;
  125. bool pol = spi->mode & SPI_CS_HIGH;
  126. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  127. if (value == BITBANG_CS_INACTIVE) {
  128. if (pdata->cs_control)
  129. pdata->cs_control(spi, !pol);
  130. }
  131. if (value == BITBANG_CS_ACTIVE) {
  132. mpc8xxx_spi->rx_shift = cs->rx_shift;
  133. mpc8xxx_spi->tx_shift = cs->tx_shift;
  134. mpc8xxx_spi->get_rx = cs->get_rx;
  135. mpc8xxx_spi->get_tx = cs->get_tx;
  136. fsl_spi_change_mode(spi);
  137. if (pdata->cs_control)
  138. pdata->cs_control(spi, pol);
  139. }
  140. }
  141. static int mspi_apply_cpu_mode_quirks(struct spi_mpc8xxx_cs *cs,
  142. struct spi_device *spi,
  143. struct mpc8xxx_spi *mpc8xxx_spi,
  144. int bits_per_word)
  145. {
  146. cs->rx_shift = 0;
  147. cs->tx_shift = 0;
  148. if (bits_per_word <= 8) {
  149. cs->get_rx = mpc8xxx_spi_rx_buf_u8;
  150. cs->get_tx = mpc8xxx_spi_tx_buf_u8;
  151. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  152. cs->rx_shift = 16;
  153. cs->tx_shift = 24;
  154. }
  155. } else if (bits_per_word <= 16) {
  156. cs->get_rx = mpc8xxx_spi_rx_buf_u16;
  157. cs->get_tx = mpc8xxx_spi_tx_buf_u16;
  158. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  159. cs->rx_shift = 16;
  160. cs->tx_shift = 16;
  161. }
  162. } else if (bits_per_word <= 32) {
  163. cs->get_rx = mpc8xxx_spi_rx_buf_u32;
  164. cs->get_tx = mpc8xxx_spi_tx_buf_u32;
  165. } else
  166. return -EINVAL;
  167. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE &&
  168. spi->mode & SPI_LSB_FIRST) {
  169. cs->tx_shift = 0;
  170. if (bits_per_word <= 8)
  171. cs->rx_shift = 8;
  172. else
  173. cs->rx_shift = 0;
  174. }
  175. mpc8xxx_spi->rx_shift = cs->rx_shift;
  176. mpc8xxx_spi->tx_shift = cs->tx_shift;
  177. mpc8xxx_spi->get_rx = cs->get_rx;
  178. mpc8xxx_spi->get_tx = cs->get_tx;
  179. return bits_per_word;
  180. }
  181. static int mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs *cs,
  182. struct spi_device *spi,
  183. int bits_per_word)
  184. {
  185. /* QE uses Little Endian for words > 8
  186. * so transform all words > 8 into 8 bits
  187. * Unfortnatly that doesn't work for LSB so
  188. * reject these for now */
  189. /* Note: 32 bits word, LSB works iff
  190. * tfcr/rfcr is set to CPMFCR_GBL */
  191. if (spi->mode & SPI_LSB_FIRST &&
  192. bits_per_word > 8)
  193. return -EINVAL;
  194. if (bits_per_word > 8)
  195. return 8; /* pretend its 8 bits */
  196. return bits_per_word;
  197. }
  198. static int fsl_spi_setup_transfer(struct spi_device *spi,
  199. struct spi_transfer *t)
  200. {
  201. struct mpc8xxx_spi *mpc8xxx_spi;
  202. int bits_per_word = 0;
  203. u8 pm;
  204. u32 hz = 0;
  205. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  206. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  207. if (t) {
  208. bits_per_word = t->bits_per_word;
  209. hz = t->speed_hz;
  210. }
  211. /* spi_transfer level calls that work per-word */
  212. if (!bits_per_word)
  213. bits_per_word = spi->bits_per_word;
  214. /* Make sure its a bit width we support [4..16, 32] */
  215. if ((bits_per_word < 4)
  216. || ((bits_per_word > 16) && (bits_per_word != 32)))
  217. return -EINVAL;
  218. if (!hz)
  219. hz = spi->max_speed_hz;
  220. if (!(mpc8xxx_spi->flags & SPI_CPM_MODE))
  221. bits_per_word = mspi_apply_cpu_mode_quirks(cs, spi,
  222. mpc8xxx_spi,
  223. bits_per_word);
  224. else if (mpc8xxx_spi->flags & SPI_QE)
  225. bits_per_word = mspi_apply_qe_mode_quirks(cs, spi,
  226. bits_per_word);
  227. if (bits_per_word < 0)
  228. return bits_per_word;
  229. if (bits_per_word == 32)
  230. bits_per_word = 0;
  231. else
  232. bits_per_word = bits_per_word - 1;
  233. /* mask out bits we are going to set */
  234. cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
  235. | SPMODE_PM(0xF));
  236. cs->hw_mode |= SPMODE_LEN(bits_per_word);
  237. if ((mpc8xxx_spi->spibrg / hz) > 64) {
  238. cs->hw_mode |= SPMODE_DIV16;
  239. pm = (mpc8xxx_spi->spibrg - 1) / (hz * 64) + 1;
  240. WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
  241. "Will use %d Hz instead.\n", dev_name(&spi->dev),
  242. hz, mpc8xxx_spi->spibrg / 1024);
  243. if (pm > 16)
  244. pm = 16;
  245. } else {
  246. pm = (mpc8xxx_spi->spibrg - 1) / (hz * 4) + 1;
  247. }
  248. if (pm)
  249. pm--;
  250. cs->hw_mode |= SPMODE_PM(pm);
  251. fsl_spi_change_mode(spi);
  252. return 0;
  253. }
  254. static void fsl_spi_cpm_bufs_start(struct mpc8xxx_spi *mspi)
  255. {
  256. struct cpm_buf_desc __iomem *tx_bd = mspi->tx_bd;
  257. struct cpm_buf_desc __iomem *rx_bd = mspi->rx_bd;
  258. unsigned int xfer_len = min(mspi->count, SPI_MRBLR);
  259. unsigned int xfer_ofs;
  260. struct fsl_spi_reg *reg_base = mspi->reg_base;
  261. xfer_ofs = mspi->xfer_in_progress->len - mspi->count;
  262. if (mspi->rx_dma == mspi->dma_dummy_rx)
  263. out_be32(&rx_bd->cbd_bufaddr, mspi->rx_dma);
  264. else
  265. out_be32(&rx_bd->cbd_bufaddr, mspi->rx_dma + xfer_ofs);
  266. out_be16(&rx_bd->cbd_datlen, 0);
  267. out_be16(&rx_bd->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT | BD_SC_WRAP);
  268. if (mspi->tx_dma == mspi->dma_dummy_tx)
  269. out_be32(&tx_bd->cbd_bufaddr, mspi->tx_dma);
  270. else
  271. out_be32(&tx_bd->cbd_bufaddr, mspi->tx_dma + xfer_ofs);
  272. out_be16(&tx_bd->cbd_datlen, xfer_len);
  273. out_be16(&tx_bd->cbd_sc, BD_SC_READY | BD_SC_INTRPT | BD_SC_WRAP |
  274. BD_SC_LAST);
  275. /* start transfer */
  276. mpc8xxx_spi_write_reg(&reg_base->command, SPCOM_STR);
  277. }
  278. static int fsl_spi_cpm_bufs(struct mpc8xxx_spi *mspi,
  279. struct spi_transfer *t, bool is_dma_mapped)
  280. {
  281. struct device *dev = mspi->dev;
  282. struct fsl_spi_reg *reg_base = mspi->reg_base;
  283. if (is_dma_mapped) {
  284. mspi->map_tx_dma = 0;
  285. mspi->map_rx_dma = 0;
  286. } else {
  287. mspi->map_tx_dma = 1;
  288. mspi->map_rx_dma = 1;
  289. }
  290. if (!t->tx_buf) {
  291. mspi->tx_dma = mspi->dma_dummy_tx;
  292. mspi->map_tx_dma = 0;
  293. }
  294. if (!t->rx_buf) {
  295. mspi->rx_dma = mspi->dma_dummy_rx;
  296. mspi->map_rx_dma = 0;
  297. }
  298. if (mspi->map_tx_dma) {
  299. void *nonconst_tx = (void *)mspi->tx; /* shut up gcc */
  300. mspi->tx_dma = dma_map_single(dev, nonconst_tx, t->len,
  301. DMA_TO_DEVICE);
  302. if (dma_mapping_error(dev, mspi->tx_dma)) {
  303. dev_err(dev, "unable to map tx dma\n");
  304. return -ENOMEM;
  305. }
  306. } else if (t->tx_buf) {
  307. mspi->tx_dma = t->tx_dma;
  308. }
  309. if (mspi->map_rx_dma) {
  310. mspi->rx_dma = dma_map_single(dev, mspi->rx, t->len,
  311. DMA_FROM_DEVICE);
  312. if (dma_mapping_error(dev, mspi->rx_dma)) {
  313. dev_err(dev, "unable to map rx dma\n");
  314. goto err_rx_dma;
  315. }
  316. } else if (t->rx_buf) {
  317. mspi->rx_dma = t->rx_dma;
  318. }
  319. /* enable rx ints */
  320. mpc8xxx_spi_write_reg(&reg_base->mask, SPIE_RXB);
  321. mspi->xfer_in_progress = t;
  322. mspi->count = t->len;
  323. /* start CPM transfers */
  324. fsl_spi_cpm_bufs_start(mspi);
  325. return 0;
  326. err_rx_dma:
  327. if (mspi->map_tx_dma)
  328. dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
  329. return -ENOMEM;
  330. }
  331. static void fsl_spi_cpm_bufs_complete(struct mpc8xxx_spi *mspi)
  332. {
  333. struct device *dev = mspi->dev;
  334. struct spi_transfer *t = mspi->xfer_in_progress;
  335. if (mspi->map_tx_dma)
  336. dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
  337. if (mspi->map_rx_dma)
  338. dma_unmap_single(dev, mspi->rx_dma, t->len, DMA_FROM_DEVICE);
  339. mspi->xfer_in_progress = NULL;
  340. }
  341. static int fsl_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
  342. struct spi_transfer *t, unsigned int len)
  343. {
  344. u32 word;
  345. struct fsl_spi_reg *reg_base = mspi->reg_base;
  346. mspi->count = len;
  347. /* enable rx ints */
  348. mpc8xxx_spi_write_reg(&reg_base->mask, SPIM_NE);
  349. /* transmit word */
  350. word = mspi->get_tx(mspi);
  351. mpc8xxx_spi_write_reg(&reg_base->transmit, word);
  352. return 0;
  353. }
  354. static int fsl_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
  355. bool is_dma_mapped)
  356. {
  357. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  358. struct fsl_spi_reg *reg_base;
  359. unsigned int len = t->len;
  360. u8 bits_per_word;
  361. int ret;
  362. reg_base = mpc8xxx_spi->reg_base;
  363. bits_per_word = spi->bits_per_word;
  364. if (t->bits_per_word)
  365. bits_per_word = t->bits_per_word;
  366. if (bits_per_word > 8) {
  367. /* invalid length? */
  368. if (len & 1)
  369. return -EINVAL;
  370. len /= 2;
  371. }
  372. if (bits_per_word > 16) {
  373. /* invalid length? */
  374. if (len & 1)
  375. return -EINVAL;
  376. len /= 2;
  377. }
  378. mpc8xxx_spi->tx = t->tx_buf;
  379. mpc8xxx_spi->rx = t->rx_buf;
  380. INIT_COMPLETION(mpc8xxx_spi->done);
  381. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  382. ret = fsl_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
  383. else
  384. ret = fsl_spi_cpu_bufs(mpc8xxx_spi, t, len);
  385. if (ret)
  386. return ret;
  387. wait_for_completion(&mpc8xxx_spi->done);
  388. /* disable rx ints */
  389. mpc8xxx_spi_write_reg(&reg_base->mask, 0);
  390. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  391. fsl_spi_cpm_bufs_complete(mpc8xxx_spi);
  392. return mpc8xxx_spi->count;
  393. }
  394. static void fsl_spi_do_one_msg(struct spi_message *m)
  395. {
  396. struct spi_device *spi = m->spi;
  397. struct spi_transfer *t;
  398. unsigned int cs_change;
  399. const int nsecs = 50;
  400. int status;
  401. cs_change = 1;
  402. status = 0;
  403. list_for_each_entry(t, &m->transfers, transfer_list) {
  404. if (t->bits_per_word || t->speed_hz) {
  405. /* Don't allow changes if CS is active */
  406. status = -EINVAL;
  407. if (cs_change)
  408. status = fsl_spi_setup_transfer(spi, t);
  409. if (status < 0)
  410. break;
  411. }
  412. if (cs_change) {
  413. fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE);
  414. ndelay(nsecs);
  415. }
  416. cs_change = t->cs_change;
  417. if (t->len)
  418. status = fsl_spi_bufs(spi, t, m->is_dma_mapped);
  419. if (status) {
  420. status = -EMSGSIZE;
  421. break;
  422. }
  423. m->actual_length += t->len;
  424. if (t->delay_usecs)
  425. udelay(t->delay_usecs);
  426. if (cs_change) {
  427. ndelay(nsecs);
  428. fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  429. ndelay(nsecs);
  430. }
  431. }
  432. m->status = status;
  433. m->complete(m->context);
  434. if (status || !cs_change) {
  435. ndelay(nsecs);
  436. fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  437. }
  438. fsl_spi_setup_transfer(spi, NULL);
  439. }
  440. static int fsl_spi_setup(struct spi_device *spi)
  441. {
  442. struct mpc8xxx_spi *mpc8xxx_spi;
  443. struct fsl_spi_reg *reg_base;
  444. int retval;
  445. u32 hw_mode;
  446. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  447. if (!spi->max_speed_hz)
  448. return -EINVAL;
  449. if (!cs) {
  450. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  451. if (!cs)
  452. return -ENOMEM;
  453. spi->controller_state = cs;
  454. }
  455. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  456. reg_base = mpc8xxx_spi->reg_base;
  457. hw_mode = cs->hw_mode; /* Save original settings */
  458. cs->hw_mode = mpc8xxx_spi_read_reg(&reg_base->mode);
  459. /* mask out bits we are going to set */
  460. cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
  461. | SPMODE_REV | SPMODE_LOOP);
  462. if (spi->mode & SPI_CPHA)
  463. cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
  464. if (spi->mode & SPI_CPOL)
  465. cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
  466. if (!(spi->mode & SPI_LSB_FIRST))
  467. cs->hw_mode |= SPMODE_REV;
  468. if (spi->mode & SPI_LOOP)
  469. cs->hw_mode |= SPMODE_LOOP;
  470. retval = fsl_spi_setup_transfer(spi, NULL);
  471. if (retval < 0) {
  472. cs->hw_mode = hw_mode; /* Restore settings */
  473. return retval;
  474. }
  475. return 0;
  476. }
  477. static void fsl_spi_cpm_irq(struct mpc8xxx_spi *mspi, u32 events)
  478. {
  479. u16 len;
  480. struct fsl_spi_reg *reg_base = mspi->reg_base;
  481. dev_dbg(mspi->dev, "%s: bd datlen %d, count %d\n", __func__,
  482. in_be16(&mspi->rx_bd->cbd_datlen), mspi->count);
  483. len = in_be16(&mspi->rx_bd->cbd_datlen);
  484. if (len > mspi->count) {
  485. WARN_ON(1);
  486. len = mspi->count;
  487. }
  488. /* Clear the events */
  489. mpc8xxx_spi_write_reg(&reg_base->event, events);
  490. mspi->count -= len;
  491. if (mspi->count)
  492. fsl_spi_cpm_bufs_start(mspi);
  493. else
  494. complete(&mspi->done);
  495. }
  496. static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
  497. {
  498. struct fsl_spi_reg *reg_base = mspi->reg_base;
  499. /* We need handle RX first */
  500. if (events & SPIE_NE) {
  501. u32 rx_data = mpc8xxx_spi_read_reg(&reg_base->receive);
  502. if (mspi->rx)
  503. mspi->get_rx(rx_data, mspi);
  504. }
  505. if ((events & SPIE_NF) == 0)
  506. /* spin until TX is done */
  507. while (((events =
  508. mpc8xxx_spi_read_reg(&reg_base->event)) &
  509. SPIE_NF) == 0)
  510. cpu_relax();
  511. /* Clear the events */
  512. mpc8xxx_spi_write_reg(&reg_base->event, events);
  513. mspi->count -= 1;
  514. if (mspi->count) {
  515. u32 word = mspi->get_tx(mspi);
  516. mpc8xxx_spi_write_reg(&reg_base->transmit, word);
  517. } else {
  518. complete(&mspi->done);
  519. }
  520. }
  521. static irqreturn_t fsl_spi_irq(s32 irq, void *context_data)
  522. {
  523. struct mpc8xxx_spi *mspi = context_data;
  524. irqreturn_t ret = IRQ_NONE;
  525. u32 events;
  526. struct fsl_spi_reg *reg_base = mspi->reg_base;
  527. /* Get interrupt events(tx/rx) */
  528. events = mpc8xxx_spi_read_reg(&reg_base->event);
  529. if (events)
  530. ret = IRQ_HANDLED;
  531. dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
  532. if (mspi->flags & SPI_CPM_MODE)
  533. fsl_spi_cpm_irq(mspi, events);
  534. else
  535. fsl_spi_cpu_irq(mspi, events);
  536. return ret;
  537. }
  538. static void *fsl_spi_alloc_dummy_rx(void)
  539. {
  540. mutex_lock(&fsl_dummy_rx_lock);
  541. if (!fsl_dummy_rx)
  542. fsl_dummy_rx = kmalloc(SPI_MRBLR, GFP_KERNEL);
  543. if (fsl_dummy_rx)
  544. fsl_dummy_rx_refcnt++;
  545. mutex_unlock(&fsl_dummy_rx_lock);
  546. return fsl_dummy_rx;
  547. }
  548. static void fsl_spi_free_dummy_rx(void)
  549. {
  550. mutex_lock(&fsl_dummy_rx_lock);
  551. switch (fsl_dummy_rx_refcnt) {
  552. case 0:
  553. WARN_ON(1);
  554. break;
  555. case 1:
  556. kfree(fsl_dummy_rx);
  557. fsl_dummy_rx = NULL;
  558. /* fall through */
  559. default:
  560. fsl_dummy_rx_refcnt--;
  561. break;
  562. }
  563. mutex_unlock(&fsl_dummy_rx_lock);
  564. }
  565. static unsigned long fsl_spi_cpm_get_pram(struct mpc8xxx_spi *mspi)
  566. {
  567. struct device *dev = mspi->dev;
  568. struct device_node *np = dev->of_node;
  569. const u32 *iprop;
  570. int size;
  571. unsigned long spi_base_ofs;
  572. unsigned long pram_ofs = -ENOMEM;
  573. /* Can't use of_address_to_resource(), QE muram isn't at 0. */
  574. iprop = of_get_property(np, "reg", &size);
  575. /* QE with a fixed pram location? */
  576. if (mspi->flags & SPI_QE && iprop && size == sizeof(*iprop) * 4)
  577. return cpm_muram_alloc_fixed(iprop[2], SPI_PRAM_SIZE);
  578. /* QE but with a dynamic pram location? */
  579. if (mspi->flags & SPI_QE) {
  580. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  581. qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, mspi->subblock,
  582. QE_CR_PROTOCOL_UNSPECIFIED, pram_ofs);
  583. return pram_ofs;
  584. }
  585. /* CPM1 and CPM2 pram must be at a fixed addr. */
  586. if (!iprop || size != sizeof(*iprop) * 4)
  587. return -ENOMEM;
  588. spi_base_ofs = cpm_muram_alloc_fixed(iprop[2], 2);
  589. if (IS_ERR_VALUE(spi_base_ofs))
  590. return -ENOMEM;
  591. if (mspi->flags & SPI_CPM2) {
  592. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  593. if (!IS_ERR_VALUE(pram_ofs)) {
  594. u16 __iomem *spi_base = cpm_muram_addr(spi_base_ofs);
  595. out_be16(spi_base, pram_ofs);
  596. }
  597. } else {
  598. struct spi_pram __iomem *pram = cpm_muram_addr(spi_base_ofs);
  599. u16 rpbase = in_be16(&pram->rpbase);
  600. /* Microcode relocation patch applied? */
  601. if (rpbase)
  602. pram_ofs = rpbase;
  603. else
  604. return spi_base_ofs;
  605. }
  606. cpm_muram_free(spi_base_ofs);
  607. return pram_ofs;
  608. }
  609. static int fsl_spi_cpm_init(struct mpc8xxx_spi *mspi)
  610. {
  611. struct device *dev = mspi->dev;
  612. struct device_node *np = dev->of_node;
  613. const u32 *iprop;
  614. int size;
  615. unsigned long pram_ofs;
  616. unsigned long bds_ofs;
  617. if (!(mspi->flags & SPI_CPM_MODE))
  618. return 0;
  619. if (!fsl_spi_alloc_dummy_rx())
  620. return -ENOMEM;
  621. if (mspi->flags & SPI_QE) {
  622. iprop = of_get_property(np, "cell-index", &size);
  623. if (iprop && size == sizeof(*iprop))
  624. mspi->subblock = *iprop;
  625. switch (mspi->subblock) {
  626. default:
  627. dev_warn(dev, "cell-index unspecified, assuming SPI1");
  628. /* fall through */
  629. case 0:
  630. mspi->subblock = QE_CR_SUBBLOCK_SPI1;
  631. break;
  632. case 1:
  633. mspi->subblock = QE_CR_SUBBLOCK_SPI2;
  634. break;
  635. }
  636. }
  637. pram_ofs = fsl_spi_cpm_get_pram(mspi);
  638. if (IS_ERR_VALUE(pram_ofs)) {
  639. dev_err(dev, "can't allocate spi parameter ram\n");
  640. goto err_pram;
  641. }
  642. bds_ofs = cpm_muram_alloc(sizeof(*mspi->tx_bd) +
  643. sizeof(*mspi->rx_bd), 8);
  644. if (IS_ERR_VALUE(bds_ofs)) {
  645. dev_err(dev, "can't allocate bds\n");
  646. goto err_bds;
  647. }
  648. mspi->dma_dummy_tx = dma_map_single(dev, empty_zero_page, PAGE_SIZE,
  649. DMA_TO_DEVICE);
  650. if (dma_mapping_error(dev, mspi->dma_dummy_tx)) {
  651. dev_err(dev, "unable to map dummy tx buffer\n");
  652. goto err_dummy_tx;
  653. }
  654. mspi->dma_dummy_rx = dma_map_single(dev, fsl_dummy_rx, SPI_MRBLR,
  655. DMA_FROM_DEVICE);
  656. if (dma_mapping_error(dev, mspi->dma_dummy_rx)) {
  657. dev_err(dev, "unable to map dummy rx buffer\n");
  658. goto err_dummy_rx;
  659. }
  660. mspi->pram = cpm_muram_addr(pram_ofs);
  661. mspi->tx_bd = cpm_muram_addr(bds_ofs);
  662. mspi->rx_bd = cpm_muram_addr(bds_ofs + sizeof(*mspi->tx_bd));
  663. /* Initialize parameter ram. */
  664. out_be16(&mspi->pram->tbase, cpm_muram_offset(mspi->tx_bd));
  665. out_be16(&mspi->pram->rbase, cpm_muram_offset(mspi->rx_bd));
  666. out_8(&mspi->pram->tfcr, CPMFCR_EB | CPMFCR_GBL);
  667. out_8(&mspi->pram->rfcr, CPMFCR_EB | CPMFCR_GBL);
  668. out_be16(&mspi->pram->mrblr, SPI_MRBLR);
  669. out_be32(&mspi->pram->rstate, 0);
  670. out_be32(&mspi->pram->rdp, 0);
  671. out_be16(&mspi->pram->rbptr, 0);
  672. out_be16(&mspi->pram->rbc, 0);
  673. out_be32(&mspi->pram->rxtmp, 0);
  674. out_be32(&mspi->pram->tstate, 0);
  675. out_be32(&mspi->pram->tdp, 0);
  676. out_be16(&mspi->pram->tbptr, 0);
  677. out_be16(&mspi->pram->tbc, 0);
  678. out_be32(&mspi->pram->txtmp, 0);
  679. return 0;
  680. err_dummy_rx:
  681. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  682. err_dummy_tx:
  683. cpm_muram_free(bds_ofs);
  684. err_bds:
  685. cpm_muram_free(pram_ofs);
  686. err_pram:
  687. fsl_spi_free_dummy_rx();
  688. return -ENOMEM;
  689. }
  690. static void fsl_spi_cpm_free(struct mpc8xxx_spi *mspi)
  691. {
  692. struct device *dev = mspi->dev;
  693. dma_unmap_single(dev, mspi->dma_dummy_rx, SPI_MRBLR, DMA_FROM_DEVICE);
  694. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  695. cpm_muram_free(cpm_muram_offset(mspi->tx_bd));
  696. cpm_muram_free(cpm_muram_offset(mspi->pram));
  697. fsl_spi_free_dummy_rx();
  698. }
  699. static void fsl_spi_remove(struct mpc8xxx_spi *mspi)
  700. {
  701. iounmap(mspi->reg_base);
  702. fsl_spi_cpm_free(mspi);
  703. }
  704. static struct spi_master * __devinit fsl_spi_probe(struct device *dev,
  705. struct resource *mem, unsigned int irq)
  706. {
  707. struct fsl_spi_platform_data *pdata = dev->platform_data;
  708. struct spi_master *master;
  709. struct mpc8xxx_spi *mpc8xxx_spi;
  710. struct fsl_spi_reg *reg_base;
  711. u32 regval;
  712. int ret = 0;
  713. master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
  714. if (master == NULL) {
  715. ret = -ENOMEM;
  716. goto err;
  717. }
  718. dev_set_drvdata(dev, master);
  719. ret = mpc8xxx_spi_probe(dev, mem, irq);
  720. if (ret)
  721. goto err_probe;
  722. master->setup = fsl_spi_setup;
  723. mpc8xxx_spi = spi_master_get_devdata(master);
  724. mpc8xxx_spi->spi_do_one_msg = fsl_spi_do_one_msg;
  725. mpc8xxx_spi->spi_remove = fsl_spi_remove;
  726. ret = fsl_spi_cpm_init(mpc8xxx_spi);
  727. if (ret)
  728. goto err_cpm_init;
  729. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  730. mpc8xxx_spi->rx_shift = 16;
  731. mpc8xxx_spi->tx_shift = 24;
  732. }
  733. mpc8xxx_spi->reg_base = ioremap(mem->start, resource_size(mem));
  734. if (mpc8xxx_spi->reg_base == NULL) {
  735. ret = -ENOMEM;
  736. goto err_ioremap;
  737. }
  738. /* Register for SPI Interrupt */
  739. ret = request_irq(mpc8xxx_spi->irq, fsl_spi_irq,
  740. 0, "fsl_spi", mpc8xxx_spi);
  741. if (ret != 0)
  742. goto free_irq;
  743. reg_base = mpc8xxx_spi->reg_base;
  744. /* SPI controller initializations */
  745. mpc8xxx_spi_write_reg(&reg_base->mode, 0);
  746. mpc8xxx_spi_write_reg(&reg_base->mask, 0);
  747. mpc8xxx_spi_write_reg(&reg_base->command, 0);
  748. mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff);
  749. /* Enable SPI interface */
  750. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  751. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
  752. regval |= SPMODE_OP;
  753. mpc8xxx_spi_write_reg(&reg_base->mode, regval);
  754. ret = spi_register_master(master);
  755. if (ret < 0)
  756. goto unreg_master;
  757. dev_info(dev, "at 0x%p (irq = %d), %s mode\n", reg_base,
  758. mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
  759. return master;
  760. unreg_master:
  761. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  762. free_irq:
  763. iounmap(mpc8xxx_spi->reg_base);
  764. err_ioremap:
  765. fsl_spi_cpm_free(mpc8xxx_spi);
  766. err_cpm_init:
  767. err_probe:
  768. spi_master_put(master);
  769. err:
  770. return ERR_PTR(ret);
  771. }
  772. static void fsl_spi_cs_control(struct spi_device *spi, bool on)
  773. {
  774. struct device *dev = spi->dev.parent;
  775. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
  776. u16 cs = spi->chip_select;
  777. int gpio = pinfo->gpios[cs];
  778. bool alow = pinfo->alow_flags[cs];
  779. gpio_set_value(gpio, on ^ alow);
  780. }
  781. static int of_fsl_spi_get_chipselects(struct device *dev)
  782. {
  783. struct device_node *np = dev->of_node;
  784. struct fsl_spi_platform_data *pdata = dev->platform_data;
  785. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  786. unsigned int ngpios;
  787. int i = 0;
  788. int ret;
  789. ngpios = of_gpio_count(np);
  790. if (!ngpios) {
  791. /*
  792. * SPI w/o chip-select line. One SPI device is still permitted
  793. * though.
  794. */
  795. pdata->max_chipselect = 1;
  796. return 0;
  797. }
  798. pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
  799. if (!pinfo->gpios)
  800. return -ENOMEM;
  801. memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
  802. pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
  803. GFP_KERNEL);
  804. if (!pinfo->alow_flags) {
  805. ret = -ENOMEM;
  806. goto err_alloc_flags;
  807. }
  808. for (; i < ngpios; i++) {
  809. int gpio;
  810. enum of_gpio_flags flags;
  811. gpio = of_get_gpio_flags(np, i, &flags);
  812. if (!gpio_is_valid(gpio)) {
  813. dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
  814. ret = gpio;
  815. goto err_loop;
  816. }
  817. ret = gpio_request(gpio, dev_name(dev));
  818. if (ret) {
  819. dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
  820. goto err_loop;
  821. }
  822. pinfo->gpios[i] = gpio;
  823. pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
  824. ret = gpio_direction_output(pinfo->gpios[i],
  825. pinfo->alow_flags[i]);
  826. if (ret) {
  827. dev_err(dev, "can't set output direction for gpio "
  828. "#%d: %d\n", i, ret);
  829. goto err_loop;
  830. }
  831. }
  832. pdata->max_chipselect = ngpios;
  833. pdata->cs_control = fsl_spi_cs_control;
  834. return 0;
  835. err_loop:
  836. while (i >= 0) {
  837. if (gpio_is_valid(pinfo->gpios[i]))
  838. gpio_free(pinfo->gpios[i]);
  839. i--;
  840. }
  841. kfree(pinfo->alow_flags);
  842. pinfo->alow_flags = NULL;
  843. err_alloc_flags:
  844. kfree(pinfo->gpios);
  845. pinfo->gpios = NULL;
  846. return ret;
  847. }
  848. static int of_fsl_spi_free_chipselects(struct device *dev)
  849. {
  850. struct fsl_spi_platform_data *pdata = dev->platform_data;
  851. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  852. int i;
  853. if (!pinfo->gpios)
  854. return 0;
  855. for (i = 0; i < pdata->max_chipselect; i++) {
  856. if (gpio_is_valid(pinfo->gpios[i]))
  857. gpio_free(pinfo->gpios[i]);
  858. }
  859. kfree(pinfo->gpios);
  860. kfree(pinfo->alow_flags);
  861. return 0;
  862. }
  863. static int __devinit of_fsl_spi_probe(struct platform_device *ofdev)
  864. {
  865. struct device *dev = &ofdev->dev;
  866. struct device_node *np = ofdev->dev.of_node;
  867. struct spi_master *master;
  868. struct resource mem;
  869. struct resource irq;
  870. int ret = -ENOMEM;
  871. ret = of_mpc8xxx_spi_probe(ofdev);
  872. if (ret)
  873. return ret;
  874. ret = of_fsl_spi_get_chipselects(dev);
  875. if (ret)
  876. goto err;
  877. ret = of_address_to_resource(np, 0, &mem);
  878. if (ret)
  879. goto err;
  880. ret = of_irq_to_resource(np, 0, &irq);
  881. if (!ret) {
  882. ret = -EINVAL;
  883. goto err;
  884. }
  885. master = fsl_spi_probe(dev, &mem, irq.start);
  886. if (IS_ERR(master)) {
  887. ret = PTR_ERR(master);
  888. goto err;
  889. }
  890. return 0;
  891. err:
  892. of_fsl_spi_free_chipselects(dev);
  893. return ret;
  894. }
  895. static int __devexit of_fsl_spi_remove(struct platform_device *ofdev)
  896. {
  897. int ret;
  898. ret = mpc8xxx_spi_remove(&ofdev->dev);
  899. if (ret)
  900. return ret;
  901. of_fsl_spi_free_chipselects(&ofdev->dev);
  902. return 0;
  903. }
  904. static const struct of_device_id of_fsl_spi_match[] = {
  905. { .compatible = "fsl,spi" },
  906. {}
  907. };
  908. MODULE_DEVICE_TABLE(of, of_fsl_spi_match);
  909. static struct platform_driver of_fsl_spi_driver = {
  910. .driver = {
  911. .name = "fsl_spi",
  912. .owner = THIS_MODULE,
  913. .of_match_table = of_fsl_spi_match,
  914. },
  915. .probe = of_fsl_spi_probe,
  916. .remove = __devexit_p(of_fsl_spi_remove),
  917. };
  918. #ifdef CONFIG_MPC832x_RDB
  919. /*
  920. * XXX XXX XXX
  921. * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
  922. * only. The driver should go away soon, since newer MPC8323E-RDB's device
  923. * tree can work with OpenFirmware driver. But for now we support old trees
  924. * as well.
  925. */
  926. static int __devinit plat_mpc8xxx_spi_probe(struct platform_device *pdev)
  927. {
  928. struct resource *mem;
  929. int irq;
  930. struct spi_master *master;
  931. if (!pdev->dev.platform_data)
  932. return -EINVAL;
  933. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  934. if (!mem)
  935. return -EINVAL;
  936. irq = platform_get_irq(pdev, 0);
  937. if (irq <= 0)
  938. return -EINVAL;
  939. master = fsl_spi_probe(&pdev->dev, mem, irq);
  940. if (IS_ERR(master))
  941. return PTR_ERR(master);
  942. return 0;
  943. }
  944. static int __devexit plat_mpc8xxx_spi_remove(struct platform_device *pdev)
  945. {
  946. return mpc8xxx_spi_remove(&pdev->dev);
  947. }
  948. MODULE_ALIAS("platform:mpc8xxx_spi");
  949. static struct platform_driver mpc8xxx_spi_driver = {
  950. .probe = plat_mpc8xxx_spi_probe,
  951. .remove = __devexit_p(plat_mpc8xxx_spi_remove),
  952. .driver = {
  953. .name = "mpc8xxx_spi",
  954. .owner = THIS_MODULE,
  955. },
  956. };
  957. static bool legacy_driver_failed;
  958. static void __init legacy_driver_register(void)
  959. {
  960. legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
  961. }
  962. static void __exit legacy_driver_unregister(void)
  963. {
  964. if (legacy_driver_failed)
  965. return;
  966. platform_driver_unregister(&mpc8xxx_spi_driver);
  967. }
  968. #else
  969. static void __init legacy_driver_register(void) {}
  970. static void __exit legacy_driver_unregister(void) {}
  971. #endif /* CONFIG_MPC832x_RDB */
  972. static int __init fsl_spi_init(void)
  973. {
  974. legacy_driver_register();
  975. return platform_driver_register(&of_fsl_spi_driver);
  976. }
  977. module_init(fsl_spi_init);
  978. static void __exit fsl_spi_exit(void)
  979. {
  980. platform_driver_unregister(&of_fsl_spi_driver);
  981. legacy_driver_unregister();
  982. }
  983. module_exit(fsl_spi_exit);
  984. MODULE_AUTHOR("Kumar Gala");
  985. MODULE_DESCRIPTION("Simple Freescale SPI Driver");
  986. MODULE_LICENSE("GPL");