spi_mpc83xx.c 12 KB

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