spi-bcm63xx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * Broadcom BCM63xx SPI controller support
  3. *
  4. * Copyright (C) 2009-2012 Florian Fainelli <florian@openwrt.org>
  5. * Copyright (C) 2010 Tanguy Bouzeloc <tanguy.bouzeloc@efixo.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the
  19. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/clk.h>
  24. #include <linux/io.h>
  25. #include <linux/module.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/delay.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/spi/spi.h>
  30. #include <linux/completion.h>
  31. #include <linux/err.h>
  32. #include <linux/workqueue.h>
  33. #include <linux/pm_runtime.h>
  34. #include <bcm63xx_dev_spi.h>
  35. #define PFX KBUILD_MODNAME
  36. struct bcm63xx_spi {
  37. struct completion done;
  38. void __iomem *regs;
  39. int irq;
  40. /* Platform data */
  41. u32 speed_hz;
  42. unsigned fifo_size;
  43. unsigned int msg_type_shift;
  44. unsigned int msg_ctl_width;
  45. /* Data buffers */
  46. const unsigned char *tx_ptr;
  47. unsigned char *rx_ptr;
  48. /* data iomem */
  49. u8 __iomem *tx_io;
  50. const u8 __iomem *rx_io;
  51. int remaining_bytes;
  52. struct clk *clk;
  53. struct platform_device *pdev;
  54. };
  55. static inline u8 bcm_spi_readb(struct bcm63xx_spi *bs,
  56. unsigned int offset)
  57. {
  58. return bcm_readb(bs->regs + bcm63xx_spireg(offset));
  59. }
  60. static inline u16 bcm_spi_readw(struct bcm63xx_spi *bs,
  61. unsigned int offset)
  62. {
  63. return bcm_readw(bs->regs + bcm63xx_spireg(offset));
  64. }
  65. static inline void bcm_spi_writeb(struct bcm63xx_spi *bs,
  66. u8 value, unsigned int offset)
  67. {
  68. bcm_writeb(value, bs->regs + bcm63xx_spireg(offset));
  69. }
  70. static inline void bcm_spi_writew(struct bcm63xx_spi *bs,
  71. u16 value, unsigned int offset)
  72. {
  73. bcm_writew(value, bs->regs + bcm63xx_spireg(offset));
  74. }
  75. static const unsigned bcm63xx_spi_freq_table[SPI_CLK_MASK][2] = {
  76. { 20000000, SPI_CLK_20MHZ },
  77. { 12500000, SPI_CLK_12_50MHZ },
  78. { 6250000, SPI_CLK_6_250MHZ },
  79. { 3125000, SPI_CLK_3_125MHZ },
  80. { 1563000, SPI_CLK_1_563MHZ },
  81. { 781000, SPI_CLK_0_781MHZ },
  82. { 391000, SPI_CLK_0_391MHZ }
  83. };
  84. static int bcm63xx_spi_check_transfer(struct spi_device *spi,
  85. struct spi_transfer *t)
  86. {
  87. u8 bits_per_word;
  88. bits_per_word = (t) ? t->bits_per_word : spi->bits_per_word;
  89. if (bits_per_word != 8) {
  90. dev_err(&spi->dev, "%s, unsupported bits_per_word=%d\n",
  91. __func__, bits_per_word);
  92. return -EINVAL;
  93. }
  94. if (spi->chip_select > spi->master->num_chipselect) {
  95. dev_err(&spi->dev, "%s, unsupported slave %d\n",
  96. __func__, spi->chip_select);
  97. return -EINVAL;
  98. }
  99. return 0;
  100. }
  101. static void bcm63xx_spi_setup_transfer(struct spi_device *spi,
  102. struct spi_transfer *t)
  103. {
  104. struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
  105. u32 hz;
  106. u8 clk_cfg, reg;
  107. int i;
  108. hz = (t) ? t->speed_hz : spi->max_speed_hz;
  109. /* Find the closest clock configuration */
  110. for (i = 0; i < SPI_CLK_MASK; i++) {
  111. if (hz >= bcm63xx_spi_freq_table[i][0]) {
  112. clk_cfg = bcm63xx_spi_freq_table[i][1];
  113. break;
  114. }
  115. }
  116. /* No matching configuration found, default to lowest */
  117. if (i == SPI_CLK_MASK)
  118. clk_cfg = SPI_CLK_0_391MHZ;
  119. /* clear existing clock configuration bits of the register */
  120. reg = bcm_spi_readb(bs, SPI_CLK_CFG);
  121. reg &= ~SPI_CLK_MASK;
  122. reg |= clk_cfg;
  123. bcm_spi_writeb(bs, reg, SPI_CLK_CFG);
  124. dev_dbg(&spi->dev, "Setting clock register to %02x (hz %d)\n",
  125. clk_cfg, hz);
  126. }
  127. /* the spi->mode bits understood by this driver: */
  128. #define MODEBITS (SPI_CPOL | SPI_CPHA)
  129. static int bcm63xx_spi_setup(struct spi_device *spi)
  130. {
  131. struct bcm63xx_spi *bs;
  132. int ret;
  133. bs = spi_master_get_devdata(spi->master);
  134. if (!spi->bits_per_word)
  135. spi->bits_per_word = 8;
  136. if (spi->mode & ~MODEBITS) {
  137. dev_err(&spi->dev, "%s, unsupported mode bits %x\n",
  138. __func__, spi->mode & ~MODEBITS);
  139. return -EINVAL;
  140. }
  141. ret = bcm63xx_spi_check_transfer(spi, NULL);
  142. if (ret < 0) {
  143. dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
  144. spi->mode & ~MODEBITS);
  145. return ret;
  146. }
  147. dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u nsec/bit\n",
  148. __func__, spi->mode & MODEBITS, spi->bits_per_word, 0);
  149. return 0;
  150. }
  151. /* Fill the TX FIFO with as many bytes as possible */
  152. static void bcm63xx_spi_fill_tx_fifo(struct bcm63xx_spi *bs)
  153. {
  154. u8 size;
  155. /* Fill the Tx FIFO with as many bytes as possible */
  156. size = bs->remaining_bytes < bs->fifo_size ? bs->remaining_bytes :
  157. bs->fifo_size;
  158. memcpy_toio(bs->tx_io, bs->tx_ptr, size);
  159. bs->remaining_bytes -= size;
  160. }
  161. static unsigned int bcm63xx_txrx_bufs(struct spi_device *spi,
  162. struct spi_transfer *t)
  163. {
  164. struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
  165. u16 msg_ctl;
  166. u16 cmd;
  167. /* Disable the CMD_DONE interrupt */
  168. bcm_spi_writeb(bs, 0, SPI_INT_MASK);
  169. dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n",
  170. t->tx_buf, t->rx_buf, t->len);
  171. /* Transmitter is inhibited */
  172. bs->tx_ptr = t->tx_buf;
  173. bs->rx_ptr = t->rx_buf;
  174. if (t->tx_buf) {
  175. bs->remaining_bytes = t->len;
  176. bcm63xx_spi_fill_tx_fifo(bs);
  177. }
  178. init_completion(&bs->done);
  179. /* Fill in the Message control register */
  180. msg_ctl = (t->len << SPI_BYTE_CNT_SHIFT);
  181. if (t->rx_buf && t->tx_buf)
  182. msg_ctl |= (SPI_FD_RW << bs->msg_type_shift);
  183. else if (t->rx_buf)
  184. msg_ctl |= (SPI_HD_R << bs->msg_type_shift);
  185. else if (t->tx_buf)
  186. msg_ctl |= (SPI_HD_W << bs->msg_type_shift);
  187. switch (bs->msg_ctl_width) {
  188. case 8:
  189. bcm_spi_writeb(bs, msg_ctl, SPI_MSG_CTL);
  190. break;
  191. case 16:
  192. bcm_spi_writew(bs, msg_ctl, SPI_MSG_CTL);
  193. break;
  194. }
  195. /* Issue the transfer */
  196. cmd = SPI_CMD_START_IMMEDIATE;
  197. cmd |= (0 << SPI_CMD_PREPEND_BYTE_CNT_SHIFT);
  198. cmd |= (spi->chip_select << SPI_CMD_DEVICE_ID_SHIFT);
  199. bcm_spi_writew(bs, cmd, SPI_CMD);
  200. /* Enable the CMD_DONE interrupt */
  201. bcm_spi_writeb(bs, SPI_INTR_CMD_DONE, SPI_INT_MASK);
  202. return t->len - bs->remaining_bytes;
  203. }
  204. static int bcm63xx_spi_prepare_transfer(struct spi_master *master)
  205. {
  206. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  207. pm_runtime_get_sync(&bs->pdev->dev);
  208. return 0;
  209. }
  210. static int bcm63xx_spi_unprepare_transfer(struct spi_master *master)
  211. {
  212. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  213. pm_runtime_put(&bs->pdev->dev);
  214. return 0;
  215. }
  216. static int bcm63xx_spi_transfer_one(struct spi_master *master,
  217. struct spi_message *m)
  218. {
  219. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  220. struct spi_transfer *t;
  221. struct spi_device *spi = m->spi;
  222. int status = 0;
  223. unsigned int timeout = 0;
  224. list_for_each_entry(t, &m->transfers, transfer_list) {
  225. unsigned int len = t->len;
  226. u8 rx_tail;
  227. status = bcm63xx_spi_check_transfer(spi, t);
  228. if (status < 0)
  229. goto exit;
  230. /* configure adapter for a new transfer */
  231. bcm63xx_spi_setup_transfer(spi, t);
  232. while (len) {
  233. /* send the data */
  234. len -= bcm63xx_txrx_bufs(spi, t);
  235. timeout = wait_for_completion_timeout(&bs->done, HZ);
  236. if (!timeout) {
  237. status = -ETIMEDOUT;
  238. goto exit;
  239. }
  240. /* read out all data */
  241. rx_tail = bcm_spi_readb(bs, SPI_RX_TAIL);
  242. /* Read out all the data */
  243. if (rx_tail)
  244. memcpy_fromio(bs->rx_ptr, bs->rx_io, rx_tail);
  245. }
  246. m->actual_length += t->len;
  247. }
  248. exit:
  249. m->status = status;
  250. spi_finalize_current_message(master);
  251. return 0;
  252. }
  253. /* This driver supports single master mode only. Hence
  254. * CMD_DONE is the only interrupt we care about
  255. */
  256. static irqreturn_t bcm63xx_spi_interrupt(int irq, void *dev_id)
  257. {
  258. struct spi_master *master = (struct spi_master *)dev_id;
  259. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  260. u8 intr;
  261. /* Read interupts and clear them immediately */
  262. intr = bcm_spi_readb(bs, SPI_INT_STATUS);
  263. bcm_spi_writeb(bs, SPI_INTR_CLEAR_ALL, SPI_INT_STATUS);
  264. bcm_spi_writeb(bs, 0, SPI_INT_MASK);
  265. /* A transfer completed */
  266. if (intr & SPI_INTR_CMD_DONE)
  267. complete(&bs->done);
  268. return IRQ_HANDLED;
  269. }
  270. static int __devinit bcm63xx_spi_probe(struct platform_device *pdev)
  271. {
  272. struct resource *r;
  273. struct device *dev = &pdev->dev;
  274. struct bcm63xx_spi_pdata *pdata = pdev->dev.platform_data;
  275. int irq;
  276. struct spi_master *master;
  277. struct clk *clk;
  278. struct bcm63xx_spi *bs;
  279. int ret;
  280. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  281. if (!r) {
  282. dev_err(dev, "no iomem\n");
  283. ret = -ENXIO;
  284. goto out;
  285. }
  286. irq = platform_get_irq(pdev, 0);
  287. if (irq < 0) {
  288. dev_err(dev, "no irq\n");
  289. ret = -ENXIO;
  290. goto out;
  291. }
  292. clk = clk_get(dev, "spi");
  293. if (IS_ERR(clk)) {
  294. dev_err(dev, "no clock for device\n");
  295. ret = PTR_ERR(clk);
  296. goto out;
  297. }
  298. master = spi_alloc_master(dev, sizeof(*bs));
  299. if (!master) {
  300. dev_err(dev, "out of memory\n");
  301. ret = -ENOMEM;
  302. goto out_clk;
  303. }
  304. bs = spi_master_get_devdata(master);
  305. platform_set_drvdata(pdev, master);
  306. bs->pdev = pdev;
  307. if (!devm_request_mem_region(&pdev->dev, r->start,
  308. resource_size(r), PFX)) {
  309. dev_err(dev, "iomem request failed\n");
  310. ret = -ENXIO;
  311. goto out_err;
  312. }
  313. bs->regs = devm_ioremap_nocache(&pdev->dev, r->start,
  314. resource_size(r));
  315. if (!bs->regs) {
  316. dev_err(dev, "unable to ioremap regs\n");
  317. ret = -ENOMEM;
  318. goto out_err;
  319. }
  320. bs->irq = irq;
  321. bs->clk = clk;
  322. bs->fifo_size = pdata->fifo_size;
  323. ret = devm_request_irq(&pdev->dev, irq, bcm63xx_spi_interrupt, 0,
  324. pdev->name, master);
  325. if (ret) {
  326. dev_err(dev, "unable to request irq\n");
  327. goto out_err;
  328. }
  329. master->bus_num = pdata->bus_num;
  330. master->num_chipselect = pdata->num_chipselect;
  331. master->setup = bcm63xx_spi_setup;
  332. master->prepare_transfer_hardware = bcm63xx_spi_prepare_transfer;
  333. master->unprepare_transfer_hardware = bcm63xx_spi_unprepare_transfer;
  334. master->transfer_one_message = bcm63xx_spi_transfer_one;
  335. master->mode_bits = MODEBITS;
  336. bs->speed_hz = pdata->speed_hz;
  337. bs->msg_type_shift = pdata->msg_type_shift;
  338. bs->msg_ctl_width = pdata->msg_ctl_width;
  339. bs->tx_io = (u8 *)(bs->regs + bcm63xx_spireg(SPI_MSG_DATA));
  340. bs->rx_io = (const u8 *)(bs->regs + bcm63xx_spireg(SPI_RX_DATA));
  341. switch (bs->msg_ctl_width) {
  342. case 8:
  343. case 16:
  344. break;
  345. default:
  346. dev_err(dev, "unsupported MSG_CTL width: %d\n",
  347. bs->msg_ctl_width);
  348. goto out_clk_disable;
  349. }
  350. /* Initialize hardware */
  351. clk_enable(bs->clk);
  352. bcm_spi_writeb(bs, SPI_INTR_CLEAR_ALL, SPI_INT_STATUS);
  353. /* register and we are done */
  354. ret = spi_register_master(master);
  355. if (ret) {
  356. dev_err(dev, "spi register failed\n");
  357. goto out_clk_disable;
  358. }
  359. dev_info(dev, "at 0x%08x (irq %d, FIFOs size %d)\n",
  360. r->start, irq, bs->fifo_size);
  361. return 0;
  362. out_clk_disable:
  363. clk_disable(clk);
  364. out_err:
  365. platform_set_drvdata(pdev, NULL);
  366. spi_master_put(master);
  367. out_clk:
  368. clk_put(clk);
  369. out:
  370. return ret;
  371. }
  372. static int __devexit bcm63xx_spi_remove(struct platform_device *pdev)
  373. {
  374. struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
  375. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  376. spi_unregister_master(master);
  377. /* reset spi block */
  378. bcm_spi_writeb(bs, 0, SPI_INT_MASK);
  379. /* HW shutdown */
  380. clk_disable(bs->clk);
  381. clk_put(bs->clk);
  382. platform_set_drvdata(pdev, 0);
  383. spi_master_put(master);
  384. return 0;
  385. }
  386. #ifdef CONFIG_PM
  387. static int bcm63xx_spi_suspend(struct device *dev)
  388. {
  389. struct spi_master *master =
  390. platform_get_drvdata(to_platform_device(dev));
  391. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  392. spi_master_suspend(master);
  393. clk_disable(bs->clk);
  394. return 0;
  395. }
  396. static int bcm63xx_spi_resume(struct device *dev)
  397. {
  398. struct spi_master *master =
  399. platform_get_drvdata(to_platform_device(dev));
  400. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  401. clk_enable(bs->clk);
  402. spi_master_resume(master);
  403. return 0;
  404. }
  405. static const struct dev_pm_ops bcm63xx_spi_pm_ops = {
  406. .suspend = bcm63xx_spi_suspend,
  407. .resume = bcm63xx_spi_resume,
  408. };
  409. #define BCM63XX_SPI_PM_OPS (&bcm63xx_spi_pm_ops)
  410. #else
  411. #define BCM63XX_SPI_PM_OPS NULL
  412. #endif
  413. static struct platform_driver bcm63xx_spi_driver = {
  414. .driver = {
  415. .name = "bcm63xx-spi",
  416. .owner = THIS_MODULE,
  417. .pm = BCM63XX_SPI_PM_OPS,
  418. },
  419. .probe = bcm63xx_spi_probe,
  420. .remove = __devexit_p(bcm63xx_spi_remove),
  421. };
  422. module_platform_driver(bcm63xx_spi_driver);
  423. MODULE_ALIAS("platform:bcm63xx_spi");
  424. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  425. MODULE_AUTHOR("Tanguy Bouzeloc <tanguy.bouzeloc@efixo.com>");
  426. MODULE_DESCRIPTION("Broadcom BCM63xx SPI Controller driver");
  427. MODULE_LICENSE("GPL");