spi-fsl-spi.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  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. void __iomem *spi_base;
  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. spi_base = of_iomap(np, 1);
  586. if (spi_base == NULL)
  587. return -EINVAL;
  588. if (mspi->flags & SPI_CPM2) {
  589. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  590. out_be16(spi_base, pram_ofs);
  591. } else {
  592. struct spi_pram __iomem *pram = spi_base;
  593. u16 rpbase = in_be16(&pram->rpbase);
  594. /* Microcode relocation patch applied? */
  595. if (rpbase)
  596. pram_ofs = rpbase;
  597. else {
  598. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  599. out_be16(spi_base, pram_ofs);
  600. }
  601. }
  602. iounmap(spi_base);
  603. return pram_ofs;
  604. }
  605. static int fsl_spi_cpm_init(struct mpc8xxx_spi *mspi)
  606. {
  607. struct device *dev = mspi->dev;
  608. struct device_node *np = dev->of_node;
  609. const u32 *iprop;
  610. int size;
  611. unsigned long pram_ofs;
  612. unsigned long bds_ofs;
  613. if (!(mspi->flags & SPI_CPM_MODE))
  614. return 0;
  615. if (!fsl_spi_alloc_dummy_rx())
  616. return -ENOMEM;
  617. if (mspi->flags & SPI_QE) {
  618. iprop = of_get_property(np, "cell-index", &size);
  619. if (iprop && size == sizeof(*iprop))
  620. mspi->subblock = *iprop;
  621. switch (mspi->subblock) {
  622. default:
  623. dev_warn(dev, "cell-index unspecified, assuming SPI1");
  624. /* fall through */
  625. case 0:
  626. mspi->subblock = QE_CR_SUBBLOCK_SPI1;
  627. break;
  628. case 1:
  629. mspi->subblock = QE_CR_SUBBLOCK_SPI2;
  630. break;
  631. }
  632. }
  633. pram_ofs = fsl_spi_cpm_get_pram(mspi);
  634. if (IS_ERR_VALUE(pram_ofs)) {
  635. dev_err(dev, "can't allocate spi parameter ram\n");
  636. goto err_pram;
  637. }
  638. bds_ofs = cpm_muram_alloc(sizeof(*mspi->tx_bd) +
  639. sizeof(*mspi->rx_bd), 8);
  640. if (IS_ERR_VALUE(bds_ofs)) {
  641. dev_err(dev, "can't allocate bds\n");
  642. goto err_bds;
  643. }
  644. mspi->dma_dummy_tx = dma_map_single(dev, empty_zero_page, PAGE_SIZE,
  645. DMA_TO_DEVICE);
  646. if (dma_mapping_error(dev, mspi->dma_dummy_tx)) {
  647. dev_err(dev, "unable to map dummy tx buffer\n");
  648. goto err_dummy_tx;
  649. }
  650. mspi->dma_dummy_rx = dma_map_single(dev, fsl_dummy_rx, SPI_MRBLR,
  651. DMA_FROM_DEVICE);
  652. if (dma_mapping_error(dev, mspi->dma_dummy_rx)) {
  653. dev_err(dev, "unable to map dummy rx buffer\n");
  654. goto err_dummy_rx;
  655. }
  656. mspi->pram = cpm_muram_addr(pram_ofs);
  657. mspi->tx_bd = cpm_muram_addr(bds_ofs);
  658. mspi->rx_bd = cpm_muram_addr(bds_ofs + sizeof(*mspi->tx_bd));
  659. /* Initialize parameter ram. */
  660. out_be16(&mspi->pram->tbase, cpm_muram_offset(mspi->tx_bd));
  661. out_be16(&mspi->pram->rbase, cpm_muram_offset(mspi->rx_bd));
  662. out_8(&mspi->pram->tfcr, CPMFCR_EB | CPMFCR_GBL);
  663. out_8(&mspi->pram->rfcr, CPMFCR_EB | CPMFCR_GBL);
  664. out_be16(&mspi->pram->mrblr, SPI_MRBLR);
  665. out_be32(&mspi->pram->rstate, 0);
  666. out_be32(&mspi->pram->rdp, 0);
  667. out_be16(&mspi->pram->rbptr, 0);
  668. out_be16(&mspi->pram->rbc, 0);
  669. out_be32(&mspi->pram->rxtmp, 0);
  670. out_be32(&mspi->pram->tstate, 0);
  671. out_be32(&mspi->pram->tdp, 0);
  672. out_be16(&mspi->pram->tbptr, 0);
  673. out_be16(&mspi->pram->tbc, 0);
  674. out_be32(&mspi->pram->txtmp, 0);
  675. return 0;
  676. err_dummy_rx:
  677. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  678. err_dummy_tx:
  679. cpm_muram_free(bds_ofs);
  680. err_bds:
  681. cpm_muram_free(pram_ofs);
  682. err_pram:
  683. fsl_spi_free_dummy_rx();
  684. return -ENOMEM;
  685. }
  686. static void fsl_spi_cpm_free(struct mpc8xxx_spi *mspi)
  687. {
  688. struct device *dev = mspi->dev;
  689. dma_unmap_single(dev, mspi->dma_dummy_rx, SPI_MRBLR, DMA_FROM_DEVICE);
  690. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  691. cpm_muram_free(cpm_muram_offset(mspi->tx_bd));
  692. cpm_muram_free(cpm_muram_offset(mspi->pram));
  693. fsl_spi_free_dummy_rx();
  694. }
  695. static void fsl_spi_remove(struct mpc8xxx_spi *mspi)
  696. {
  697. iounmap(mspi->reg_base);
  698. fsl_spi_cpm_free(mspi);
  699. }
  700. static struct spi_master * __devinit fsl_spi_probe(struct device *dev,
  701. struct resource *mem, unsigned int irq)
  702. {
  703. struct fsl_spi_platform_data *pdata = dev->platform_data;
  704. struct spi_master *master;
  705. struct mpc8xxx_spi *mpc8xxx_spi;
  706. struct fsl_spi_reg *reg_base;
  707. u32 regval;
  708. int ret = 0;
  709. master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
  710. if (master == NULL) {
  711. ret = -ENOMEM;
  712. goto err;
  713. }
  714. dev_set_drvdata(dev, master);
  715. ret = mpc8xxx_spi_probe(dev, mem, irq);
  716. if (ret)
  717. goto err_probe;
  718. master->setup = fsl_spi_setup;
  719. mpc8xxx_spi = spi_master_get_devdata(master);
  720. mpc8xxx_spi->spi_do_one_msg = fsl_spi_do_one_msg;
  721. mpc8xxx_spi->spi_remove = fsl_spi_remove;
  722. ret = fsl_spi_cpm_init(mpc8xxx_spi);
  723. if (ret)
  724. goto err_cpm_init;
  725. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  726. mpc8xxx_spi->rx_shift = 16;
  727. mpc8xxx_spi->tx_shift = 24;
  728. }
  729. mpc8xxx_spi->reg_base = ioremap(mem->start, resource_size(mem));
  730. if (mpc8xxx_spi->reg_base == NULL) {
  731. ret = -ENOMEM;
  732. goto err_ioremap;
  733. }
  734. /* Register for SPI Interrupt */
  735. ret = request_irq(mpc8xxx_spi->irq, fsl_spi_irq,
  736. 0, "fsl_spi", mpc8xxx_spi);
  737. if (ret != 0)
  738. goto free_irq;
  739. reg_base = mpc8xxx_spi->reg_base;
  740. /* SPI controller initializations */
  741. mpc8xxx_spi_write_reg(&reg_base->mode, 0);
  742. mpc8xxx_spi_write_reg(&reg_base->mask, 0);
  743. mpc8xxx_spi_write_reg(&reg_base->command, 0);
  744. mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff);
  745. /* Enable SPI interface */
  746. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  747. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
  748. regval |= SPMODE_OP;
  749. mpc8xxx_spi_write_reg(&reg_base->mode, regval);
  750. ret = spi_register_master(master);
  751. if (ret < 0)
  752. goto unreg_master;
  753. dev_info(dev, "at 0x%p (irq = %d), %s mode\n", reg_base,
  754. mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
  755. return master;
  756. unreg_master:
  757. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  758. free_irq:
  759. iounmap(mpc8xxx_spi->reg_base);
  760. err_ioremap:
  761. fsl_spi_cpm_free(mpc8xxx_spi);
  762. err_cpm_init:
  763. err_probe:
  764. spi_master_put(master);
  765. err:
  766. return ERR_PTR(ret);
  767. }
  768. static void fsl_spi_cs_control(struct spi_device *spi, bool on)
  769. {
  770. struct device *dev = spi->dev.parent;
  771. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
  772. u16 cs = spi->chip_select;
  773. int gpio = pinfo->gpios[cs];
  774. bool alow = pinfo->alow_flags[cs];
  775. gpio_set_value(gpio, on ^ alow);
  776. }
  777. static int of_fsl_spi_get_chipselects(struct device *dev)
  778. {
  779. struct device_node *np = dev->of_node;
  780. struct fsl_spi_platform_data *pdata = dev->platform_data;
  781. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  782. unsigned int ngpios;
  783. int i = 0;
  784. int ret;
  785. ngpios = of_gpio_count(np);
  786. if (!ngpios) {
  787. /*
  788. * SPI w/o chip-select line. One SPI device is still permitted
  789. * though.
  790. */
  791. pdata->max_chipselect = 1;
  792. return 0;
  793. }
  794. pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
  795. if (!pinfo->gpios)
  796. return -ENOMEM;
  797. memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
  798. pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
  799. GFP_KERNEL);
  800. if (!pinfo->alow_flags) {
  801. ret = -ENOMEM;
  802. goto err_alloc_flags;
  803. }
  804. for (; i < ngpios; i++) {
  805. int gpio;
  806. enum of_gpio_flags flags;
  807. gpio = of_get_gpio_flags(np, i, &flags);
  808. if (!gpio_is_valid(gpio)) {
  809. dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
  810. ret = gpio;
  811. goto err_loop;
  812. }
  813. ret = gpio_request(gpio, dev_name(dev));
  814. if (ret) {
  815. dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
  816. goto err_loop;
  817. }
  818. pinfo->gpios[i] = gpio;
  819. pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
  820. ret = gpio_direction_output(pinfo->gpios[i],
  821. pinfo->alow_flags[i]);
  822. if (ret) {
  823. dev_err(dev, "can't set output direction for gpio "
  824. "#%d: %d\n", i, ret);
  825. goto err_loop;
  826. }
  827. }
  828. pdata->max_chipselect = ngpios;
  829. pdata->cs_control = fsl_spi_cs_control;
  830. return 0;
  831. err_loop:
  832. while (i >= 0) {
  833. if (gpio_is_valid(pinfo->gpios[i]))
  834. gpio_free(pinfo->gpios[i]);
  835. i--;
  836. }
  837. kfree(pinfo->alow_flags);
  838. pinfo->alow_flags = NULL;
  839. err_alloc_flags:
  840. kfree(pinfo->gpios);
  841. pinfo->gpios = NULL;
  842. return ret;
  843. }
  844. static int of_fsl_spi_free_chipselects(struct device *dev)
  845. {
  846. struct fsl_spi_platform_data *pdata = dev->platform_data;
  847. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  848. int i;
  849. if (!pinfo->gpios)
  850. return 0;
  851. for (i = 0; i < pdata->max_chipselect; i++) {
  852. if (gpio_is_valid(pinfo->gpios[i]))
  853. gpio_free(pinfo->gpios[i]);
  854. }
  855. kfree(pinfo->gpios);
  856. kfree(pinfo->alow_flags);
  857. return 0;
  858. }
  859. static int __devinit of_fsl_spi_probe(struct platform_device *ofdev)
  860. {
  861. struct device *dev = &ofdev->dev;
  862. struct device_node *np = ofdev->dev.of_node;
  863. struct spi_master *master;
  864. struct resource mem;
  865. struct resource irq;
  866. int ret = -ENOMEM;
  867. ret = of_mpc8xxx_spi_probe(ofdev);
  868. if (ret)
  869. return ret;
  870. ret = of_fsl_spi_get_chipselects(dev);
  871. if (ret)
  872. goto err;
  873. ret = of_address_to_resource(np, 0, &mem);
  874. if (ret)
  875. goto err;
  876. ret = of_irq_to_resource(np, 0, &irq);
  877. if (!ret) {
  878. ret = -EINVAL;
  879. goto err;
  880. }
  881. master = fsl_spi_probe(dev, &mem, irq.start);
  882. if (IS_ERR(master)) {
  883. ret = PTR_ERR(master);
  884. goto err;
  885. }
  886. return 0;
  887. err:
  888. of_fsl_spi_free_chipselects(dev);
  889. return ret;
  890. }
  891. static int __devexit of_fsl_spi_remove(struct platform_device *ofdev)
  892. {
  893. int ret;
  894. ret = mpc8xxx_spi_remove(&ofdev->dev);
  895. if (ret)
  896. return ret;
  897. of_fsl_spi_free_chipselects(&ofdev->dev);
  898. return 0;
  899. }
  900. static const struct of_device_id of_fsl_spi_match[] = {
  901. { .compatible = "fsl,spi" },
  902. {}
  903. };
  904. MODULE_DEVICE_TABLE(of, of_fsl_spi_match);
  905. static struct platform_driver of_fsl_spi_driver = {
  906. .driver = {
  907. .name = "fsl_spi",
  908. .owner = THIS_MODULE,
  909. .of_match_table = of_fsl_spi_match,
  910. },
  911. .probe = of_fsl_spi_probe,
  912. .remove = __devexit_p(of_fsl_spi_remove),
  913. };
  914. #ifdef CONFIG_MPC832x_RDB
  915. /*
  916. * XXX XXX XXX
  917. * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
  918. * only. The driver should go away soon, since newer MPC8323E-RDB's device
  919. * tree can work with OpenFirmware driver. But for now we support old trees
  920. * as well.
  921. */
  922. static int __devinit plat_mpc8xxx_spi_probe(struct platform_device *pdev)
  923. {
  924. struct resource *mem;
  925. int irq;
  926. struct spi_master *master;
  927. if (!pdev->dev.platform_data)
  928. return -EINVAL;
  929. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  930. if (!mem)
  931. return -EINVAL;
  932. irq = platform_get_irq(pdev, 0);
  933. if (irq <= 0)
  934. return -EINVAL;
  935. master = fsl_spi_probe(&pdev->dev, mem, irq);
  936. if (IS_ERR(master))
  937. return PTR_ERR(master);
  938. return 0;
  939. }
  940. static int __devexit plat_mpc8xxx_spi_remove(struct platform_device *pdev)
  941. {
  942. return mpc8xxx_spi_remove(&pdev->dev);
  943. }
  944. MODULE_ALIAS("platform:mpc8xxx_spi");
  945. static struct platform_driver mpc8xxx_spi_driver = {
  946. .probe = plat_mpc8xxx_spi_probe,
  947. .remove = __devexit_p(plat_mpc8xxx_spi_remove),
  948. .driver = {
  949. .name = "mpc8xxx_spi",
  950. .owner = THIS_MODULE,
  951. },
  952. };
  953. static bool legacy_driver_failed;
  954. static void __init legacy_driver_register(void)
  955. {
  956. legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
  957. }
  958. static void __exit legacy_driver_unregister(void)
  959. {
  960. if (legacy_driver_failed)
  961. return;
  962. platform_driver_unregister(&mpc8xxx_spi_driver);
  963. }
  964. #else
  965. static void __init legacy_driver_register(void) {}
  966. static void __exit legacy_driver_unregister(void) {}
  967. #endif /* CONFIG_MPC832x_RDB */
  968. static int __init fsl_spi_init(void)
  969. {
  970. legacy_driver_register();
  971. return platform_driver_register(&of_fsl_spi_driver);
  972. }
  973. module_init(fsl_spi_init);
  974. static void __exit fsl_spi_exit(void)
  975. {
  976. platform_driver_unregister(&of_fsl_spi_driver);
  977. legacy_driver_unregister();
  978. }
  979. module_exit(fsl_spi_exit);
  980. MODULE_AUTHOR("Kumar Gala");
  981. MODULE_DESCRIPTION("Simple Freescale SPI Driver");
  982. MODULE_LICENSE("GPL");