spi-fsl-spi.c 28 KB

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