spi_mpc83xx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * MPC83xx SPI controller driver.
  3. *
  4. * Maintainer: Kumar Gala
  5. *
  6. * Copyright (C) 2006 Polycom, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/completion.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/delay.h>
  20. #include <linux/irq.h>
  21. #include <linux/device.h>
  22. #include <linux/spi/spi.h>
  23. #include <linux/spi/spi_bitbang.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/fsl_devices.h>
  26. #include <asm/irq.h>
  27. #include <asm/io.h>
  28. /* SPI Controller registers */
  29. struct mpc83xx_spi_reg {
  30. u8 res1[0x20];
  31. __be32 mode;
  32. __be32 event;
  33. __be32 mask;
  34. __be32 command;
  35. __be32 transmit;
  36. __be32 receive;
  37. };
  38. /* SPI Controller mode register definitions */
  39. #define SPMODE_LOOP (1 << 30)
  40. #define SPMODE_CI_INACTIVEHIGH (1 << 29)
  41. #define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
  42. #define SPMODE_DIV16 (1 << 27)
  43. #define SPMODE_REV (1 << 26)
  44. #define SPMODE_MS (1 << 25)
  45. #define SPMODE_ENABLE (1 << 24)
  46. #define SPMODE_LEN(x) ((x) << 20)
  47. #define SPMODE_PM(x) ((x) << 16)
  48. #define SPMODE_OP (1 << 14)
  49. /*
  50. * Default for SPI Mode:
  51. * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
  52. */
  53. #define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
  54. SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
  55. /* SPIE register values */
  56. #define SPIE_NE 0x00000200 /* Not empty */
  57. #define SPIE_NF 0x00000100 /* Not full */
  58. /* SPIM register values */
  59. #define SPIM_NE 0x00000200 /* Not empty */
  60. #define SPIM_NF 0x00000100 /* Not full */
  61. /* SPI Controller driver's private data. */
  62. struct mpc83xx_spi {
  63. /* bitbang has to be first */
  64. struct spi_bitbang bitbang;
  65. struct completion done;
  66. struct mpc83xx_spi_reg __iomem *base;
  67. /* rx & tx bufs from the spi_transfer */
  68. const void *tx;
  69. void *rx;
  70. /* functions to deal with different sized buffers */
  71. void (*get_rx) (u32 rx_data, struct mpc83xx_spi *);
  72. u32(*get_tx) (struct mpc83xx_spi *);
  73. unsigned int count;
  74. u32 irq;
  75. unsigned nsecs; /* (clock cycle time)/2 */
  76. u32 spibrg; /* SPIBRG input clock */
  77. u32 rx_shift; /* RX data reg shift when in qe mode */
  78. u32 tx_shift; /* TX data reg shift when in qe mode */
  79. bool qe_mode;
  80. void (*activate_cs) (u8 cs, u8 polarity);
  81. void (*deactivate_cs) (u8 cs, u8 polarity);
  82. };
  83. static inline void mpc83xx_spi_write_reg(__be32 __iomem * reg, u32 val)
  84. {
  85. out_be32(reg, val);
  86. }
  87. static inline u32 mpc83xx_spi_read_reg(__be32 __iomem * reg)
  88. {
  89. return in_be32(reg);
  90. }
  91. #define MPC83XX_SPI_RX_BUF(type) \
  92. void mpc83xx_spi_rx_buf_##type(u32 data, struct mpc83xx_spi *mpc83xx_spi) \
  93. { \
  94. type * rx = mpc83xx_spi->rx; \
  95. *rx++ = (type)(data >> mpc83xx_spi->rx_shift); \
  96. mpc83xx_spi->rx = rx; \
  97. }
  98. #define MPC83XX_SPI_TX_BUF(type) \
  99. u32 mpc83xx_spi_tx_buf_##type(struct mpc83xx_spi *mpc83xx_spi) \
  100. { \
  101. u32 data; \
  102. const type * tx = mpc83xx_spi->tx; \
  103. if (!tx) \
  104. return 0; \
  105. data = *tx++ << mpc83xx_spi->tx_shift; \
  106. mpc83xx_spi->tx = tx; \
  107. return data; \
  108. }
  109. MPC83XX_SPI_RX_BUF(u8)
  110. MPC83XX_SPI_RX_BUF(u16)
  111. MPC83XX_SPI_RX_BUF(u32)
  112. MPC83XX_SPI_TX_BUF(u8)
  113. MPC83XX_SPI_TX_BUF(u16)
  114. MPC83XX_SPI_TX_BUF(u32)
  115. static void mpc83xx_spi_chipselect(struct spi_device *spi, int value)
  116. {
  117. struct mpc83xx_spi *mpc83xx_spi;
  118. u8 pol = spi->mode & SPI_CS_HIGH ? 1 : 0;
  119. mpc83xx_spi = spi_master_get_devdata(spi->master);
  120. if (value == BITBANG_CS_INACTIVE) {
  121. if (mpc83xx_spi->deactivate_cs)
  122. mpc83xx_spi->deactivate_cs(spi->chip_select, pol);
  123. }
  124. if (value == BITBANG_CS_ACTIVE) {
  125. u32 regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
  126. u32 len = spi->bits_per_word;
  127. u8 pm;
  128. if (len == 32)
  129. len = 0;
  130. else
  131. len = len - 1;
  132. /* mask out bits we are going to set */
  133. regval &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
  134. | SPMODE_LEN(0xF) | SPMODE_DIV16
  135. | SPMODE_PM(0xF) | SPMODE_REV | SPMODE_LOOP);
  136. if (spi->mode & SPI_CPHA)
  137. regval |= SPMODE_CP_BEGIN_EDGECLK;
  138. if (spi->mode & SPI_CPOL)
  139. regval |= SPMODE_CI_INACTIVEHIGH;
  140. if (!(spi->mode & SPI_LSB_FIRST))
  141. regval |= SPMODE_REV;
  142. if (spi->mode & SPI_LOOP)
  143. regval |= SPMODE_LOOP;
  144. regval |= SPMODE_LEN(len);
  145. if ((mpc83xx_spi->spibrg / spi->max_speed_hz) >= 64) {
  146. pm = mpc83xx_spi->spibrg / (spi->max_speed_hz * 64) - 1;
  147. if (pm > 0x0f) {
  148. dev_err(&spi->dev, "Requested speed is too "
  149. "low: %d Hz. Will use %d Hz instead.\n",
  150. spi->max_speed_hz,
  151. mpc83xx_spi->spibrg / 1024);
  152. pm = 0x0f;
  153. }
  154. regval |= SPMODE_PM(pm) | SPMODE_DIV16;
  155. } else {
  156. pm = mpc83xx_spi->spibrg / (spi->max_speed_hz * 4);
  157. if (pm)
  158. pm--;
  159. regval |= SPMODE_PM(pm);
  160. }
  161. /* Turn off SPI unit prior changing mode */
  162. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, 0);
  163. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, regval);
  164. if (mpc83xx_spi->activate_cs)
  165. mpc83xx_spi->activate_cs(spi->chip_select, pol);
  166. }
  167. }
  168. static
  169. int mpc83xx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  170. {
  171. struct mpc83xx_spi *mpc83xx_spi;
  172. u32 regval;
  173. u8 bits_per_word;
  174. u32 hz;
  175. mpc83xx_spi = spi_master_get_devdata(spi->master);
  176. if (t) {
  177. bits_per_word = t->bits_per_word;
  178. hz = t->speed_hz;
  179. } else {
  180. bits_per_word = 0;
  181. hz = 0;
  182. }
  183. /* spi_transfer level calls that work per-word */
  184. if (!bits_per_word)
  185. bits_per_word = spi->bits_per_word;
  186. /* Make sure its a bit width we support [4..16, 32] */
  187. if ((bits_per_word < 4)
  188. || ((bits_per_word > 16) && (bits_per_word != 32)))
  189. return -EINVAL;
  190. mpc83xx_spi->rx_shift = 0;
  191. mpc83xx_spi->tx_shift = 0;
  192. if (bits_per_word <= 8) {
  193. mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u8;
  194. mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u8;
  195. if (mpc83xx_spi->qe_mode) {
  196. mpc83xx_spi->rx_shift = 16;
  197. mpc83xx_spi->tx_shift = 24;
  198. }
  199. } else if (bits_per_word <= 16) {
  200. mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u16;
  201. mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u16;
  202. if (mpc83xx_spi->qe_mode) {
  203. mpc83xx_spi->rx_shift = 16;
  204. mpc83xx_spi->tx_shift = 16;
  205. }
  206. } else if (bits_per_word <= 32) {
  207. mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u32;
  208. mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u32;
  209. } else
  210. return -EINVAL;
  211. if (mpc83xx_spi->qe_mode && spi->mode & SPI_LSB_FIRST) {
  212. mpc83xx_spi->tx_shift = 0;
  213. if (bits_per_word <= 8)
  214. mpc83xx_spi->rx_shift = 8;
  215. else
  216. mpc83xx_spi->rx_shift = 0;
  217. }
  218. /* nsecs = (clock period)/2 */
  219. if (!hz)
  220. hz = spi->max_speed_hz;
  221. mpc83xx_spi->nsecs = (1000000000 / 2) / hz;
  222. if (mpc83xx_spi->nsecs > MAX_UDELAY_MS * 1000)
  223. return -EINVAL;
  224. if (bits_per_word == 32)
  225. bits_per_word = 0;
  226. else
  227. bits_per_word = bits_per_word - 1;
  228. regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
  229. /* mask out bits we are going to set */
  230. regval &= ~(SPMODE_LEN(0xF) | SPMODE_REV);
  231. regval |= SPMODE_LEN(bits_per_word);
  232. if (!(spi->mode & SPI_LSB_FIRST))
  233. regval |= SPMODE_REV;
  234. /* Turn off SPI unit prior changing mode */
  235. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, 0);
  236. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, regval);
  237. return 0;
  238. }
  239. /* the spi->mode bits understood by this driver: */
  240. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
  241. | SPI_LSB_FIRST | SPI_LOOP)
  242. static int mpc83xx_spi_setup(struct spi_device *spi)
  243. {
  244. struct spi_bitbang *bitbang;
  245. struct mpc83xx_spi *mpc83xx_spi;
  246. int retval;
  247. if (spi->mode & ~MODEBITS) {
  248. dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
  249. spi->mode & ~MODEBITS);
  250. return -EINVAL;
  251. }
  252. if (!spi->max_speed_hz)
  253. return -EINVAL;
  254. bitbang = spi_master_get_devdata(spi->master);
  255. mpc83xx_spi = spi_master_get_devdata(spi->master);
  256. if (!spi->bits_per_word)
  257. spi->bits_per_word = 8;
  258. retval = mpc83xx_spi_setup_transfer(spi, NULL);
  259. if (retval < 0)
  260. return retval;
  261. dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u nsec\n",
  262. __FUNCTION__, spi->mode & (SPI_CPOL | SPI_CPHA),
  263. spi->bits_per_word, 2 * mpc83xx_spi->nsecs);
  264. /* NOTE we _need_ to call chipselect() early, ideally with adapter
  265. * setup, unless the hardware defaults cooperate to avoid confusion
  266. * between normal (active low) and inverted chipselects.
  267. */
  268. /* deselect chip (low or high) */
  269. spin_lock(&bitbang->lock);
  270. if (!bitbang->busy) {
  271. bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  272. ndelay(mpc83xx_spi->nsecs);
  273. }
  274. spin_unlock(&bitbang->lock);
  275. return 0;
  276. }
  277. static int mpc83xx_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
  278. {
  279. struct mpc83xx_spi *mpc83xx_spi;
  280. u32 word;
  281. mpc83xx_spi = spi_master_get_devdata(spi->master);
  282. mpc83xx_spi->tx = t->tx_buf;
  283. mpc83xx_spi->rx = t->rx_buf;
  284. mpc83xx_spi->count = t->len;
  285. INIT_COMPLETION(mpc83xx_spi->done);
  286. /* enable rx ints */
  287. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, SPIM_NE);
  288. /* transmit word */
  289. word = mpc83xx_spi->get_tx(mpc83xx_spi);
  290. mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit, word);
  291. wait_for_completion(&mpc83xx_spi->done);
  292. /* disable rx ints */
  293. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
  294. return t->len - mpc83xx_spi->count;
  295. }
  296. irqreturn_t mpc83xx_spi_irq(s32 irq, void *context_data)
  297. {
  298. struct mpc83xx_spi *mpc83xx_spi = context_data;
  299. u32 event;
  300. irqreturn_t ret = IRQ_NONE;
  301. /* Get interrupt events(tx/rx) */
  302. event = mpc83xx_spi_read_reg(&mpc83xx_spi->base->event);
  303. /* We need handle RX first */
  304. if (event & SPIE_NE) {
  305. u32 rx_data = mpc83xx_spi_read_reg(&mpc83xx_spi->base->receive);
  306. if (mpc83xx_spi->rx)
  307. mpc83xx_spi->get_rx(rx_data, mpc83xx_spi);
  308. ret = IRQ_HANDLED;
  309. }
  310. if ((event & SPIE_NF) == 0)
  311. /* spin until TX is done */
  312. while (((event =
  313. mpc83xx_spi_read_reg(&mpc83xx_spi->base->event)) &
  314. SPIE_NF) == 0)
  315. cpu_relax();
  316. mpc83xx_spi->count -= 1;
  317. if (mpc83xx_spi->count) {
  318. u32 word = mpc83xx_spi->get_tx(mpc83xx_spi);
  319. mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit, word);
  320. } else {
  321. complete(&mpc83xx_spi->done);
  322. }
  323. /* Clear the events */
  324. mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, event);
  325. return ret;
  326. }
  327. static int __init mpc83xx_spi_probe(struct platform_device *dev)
  328. {
  329. struct spi_master *master;
  330. struct mpc83xx_spi *mpc83xx_spi;
  331. struct fsl_spi_platform_data *pdata;
  332. struct resource *r;
  333. u32 regval;
  334. int ret = 0;
  335. /* Get resources(memory, IRQ) associated with the device */
  336. master = spi_alloc_master(&dev->dev, sizeof(struct mpc83xx_spi));
  337. if (master == NULL) {
  338. ret = -ENOMEM;
  339. goto err;
  340. }
  341. platform_set_drvdata(dev, master);
  342. pdata = dev->dev.platform_data;
  343. if (pdata == NULL) {
  344. ret = -ENODEV;
  345. goto free_master;
  346. }
  347. r = platform_get_resource(dev, IORESOURCE_MEM, 0);
  348. if (r == NULL) {
  349. ret = -ENODEV;
  350. goto free_master;
  351. }
  352. mpc83xx_spi = spi_master_get_devdata(master);
  353. mpc83xx_spi->bitbang.master = spi_master_get(master);
  354. mpc83xx_spi->bitbang.chipselect = mpc83xx_spi_chipselect;
  355. mpc83xx_spi->bitbang.setup_transfer = mpc83xx_spi_setup_transfer;
  356. mpc83xx_spi->bitbang.txrx_bufs = mpc83xx_spi_bufs;
  357. mpc83xx_spi->activate_cs = pdata->activate_cs;
  358. mpc83xx_spi->deactivate_cs = pdata->deactivate_cs;
  359. mpc83xx_spi->qe_mode = pdata->qe_mode;
  360. mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u8;
  361. mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u8;
  362. if (mpc83xx_spi->qe_mode)
  363. mpc83xx_spi->spibrg = pdata->sysclk / 2;
  364. else
  365. mpc83xx_spi->spibrg = pdata->sysclk;
  366. mpc83xx_spi->rx_shift = 0;
  367. mpc83xx_spi->tx_shift = 0;
  368. if (mpc83xx_spi->qe_mode) {
  369. mpc83xx_spi->rx_shift = 16;
  370. mpc83xx_spi->tx_shift = 24;
  371. }
  372. mpc83xx_spi->bitbang.master->setup = mpc83xx_spi_setup;
  373. init_completion(&mpc83xx_spi->done);
  374. mpc83xx_spi->base = ioremap(r->start, r->end - r->start + 1);
  375. if (mpc83xx_spi->base == NULL) {
  376. ret = -ENOMEM;
  377. goto put_master;
  378. }
  379. mpc83xx_spi->irq = platform_get_irq(dev, 0);
  380. if (mpc83xx_spi->irq < 0) {
  381. ret = -ENXIO;
  382. goto unmap_io;
  383. }
  384. /* Register for SPI Interrupt */
  385. ret = request_irq(mpc83xx_spi->irq, mpc83xx_spi_irq,
  386. 0, "mpc83xx_spi", mpc83xx_spi);
  387. if (ret != 0)
  388. goto unmap_io;
  389. master->bus_num = pdata->bus_num;
  390. master->num_chipselect = pdata->max_chipselect;
  391. /* SPI controller initializations */
  392. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, 0);
  393. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
  394. mpc83xx_spi_write_reg(&mpc83xx_spi->base->command, 0);
  395. mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, 0xffffffff);
  396. /* Enable SPI interface */
  397. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  398. if (pdata->qe_mode)
  399. regval |= SPMODE_OP;
  400. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, regval);
  401. ret = spi_bitbang_start(&mpc83xx_spi->bitbang);
  402. if (ret != 0)
  403. goto free_irq;
  404. printk(KERN_INFO
  405. "%s: MPC83xx SPI Controller driver at 0x%p (irq = %d)\n",
  406. dev->dev.bus_id, mpc83xx_spi->base, mpc83xx_spi->irq);
  407. return ret;
  408. free_irq:
  409. free_irq(mpc83xx_spi->irq, mpc83xx_spi);
  410. unmap_io:
  411. iounmap(mpc83xx_spi->base);
  412. put_master:
  413. spi_master_put(master);
  414. free_master:
  415. kfree(master);
  416. err:
  417. return ret;
  418. }
  419. static int __exit mpc83xx_spi_remove(struct platform_device *dev)
  420. {
  421. struct mpc83xx_spi *mpc83xx_spi;
  422. struct spi_master *master;
  423. master = platform_get_drvdata(dev);
  424. mpc83xx_spi = spi_master_get_devdata(master);
  425. spi_bitbang_stop(&mpc83xx_spi->bitbang);
  426. free_irq(mpc83xx_spi->irq, mpc83xx_spi);
  427. iounmap(mpc83xx_spi->base);
  428. spi_master_put(mpc83xx_spi->bitbang.master);
  429. return 0;
  430. }
  431. MODULE_ALIAS("mpc83xx_spi"); /* for platform bus hotplug */
  432. static struct platform_driver mpc83xx_spi_driver = {
  433. .remove = __exit_p(mpc83xx_spi_remove),
  434. .driver = {
  435. .name = "mpc83xx_spi",
  436. },
  437. };
  438. static int __init mpc83xx_spi_init(void)
  439. {
  440. return platform_driver_probe(&mpc83xx_spi_driver, mpc83xx_spi_probe);
  441. }
  442. static void __exit mpc83xx_spi_exit(void)
  443. {
  444. platform_driver_unregister(&mpc83xx_spi_driver);
  445. }
  446. module_init(mpc83xx_spi_init);
  447. module_exit(mpc83xx_spi_exit);
  448. MODULE_AUTHOR("Kumar Gala");
  449. MODULE_DESCRIPTION("Simple MPC83xx SPI Driver");
  450. MODULE_LICENSE("GPL");