spi-sh.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * SH SPI bus driver
  3. *
  4. * Copyright (C) 2011 Renesas Solutions Corp.
  5. *
  6. * Based on pxa2xx_spi.c:
  7. * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/sched.h>
  21. #include <linux/errno.h>
  22. #include <linux/timer.h>
  23. #include <linux/delay.h>
  24. #include <linux/list.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/io.h>
  29. #include <linux/spi/spi.h>
  30. #define SPI_SH_TBR 0x00
  31. #define SPI_SH_RBR 0x00
  32. #define SPI_SH_CR1 0x08
  33. #define SPI_SH_CR2 0x10
  34. #define SPI_SH_CR3 0x18
  35. #define SPI_SH_CR4 0x20
  36. #define SPI_SH_CR5 0x28
  37. /* CR1 */
  38. #define SPI_SH_TBE 0x80
  39. #define SPI_SH_TBF 0x40
  40. #define SPI_SH_RBE 0x20
  41. #define SPI_SH_RBF 0x10
  42. #define SPI_SH_PFONRD 0x08
  43. #define SPI_SH_SSDB 0x04
  44. #define SPI_SH_SSD 0x02
  45. #define SPI_SH_SSA 0x01
  46. /* CR2 */
  47. #define SPI_SH_RSTF 0x80
  48. #define SPI_SH_LOOPBK 0x40
  49. #define SPI_SH_CPOL 0x20
  50. #define SPI_SH_CPHA 0x10
  51. #define SPI_SH_L1M0 0x08
  52. /* CR3 */
  53. #define SPI_SH_MAX_BYTE 0xFF
  54. /* CR4 */
  55. #define SPI_SH_TBEI 0x80
  56. #define SPI_SH_TBFI 0x40
  57. #define SPI_SH_RBEI 0x20
  58. #define SPI_SH_RBFI 0x10
  59. #define SPI_SH_WPABRT 0x04
  60. #define SPI_SH_SSS 0x01
  61. /* CR8 */
  62. #define SPI_SH_P1L0 0x80
  63. #define SPI_SH_PP1L0 0x40
  64. #define SPI_SH_MUXI 0x20
  65. #define SPI_SH_MUXIRQ 0x10
  66. #define SPI_SH_FIFO_SIZE 32
  67. #define SPI_SH_SEND_TIMEOUT (3 * HZ)
  68. #define SPI_SH_RECEIVE_TIMEOUT (HZ >> 3)
  69. #undef DEBUG
  70. struct spi_sh_data {
  71. void __iomem *addr;
  72. int irq;
  73. struct spi_master *master;
  74. struct list_head queue;
  75. struct workqueue_struct *workqueue;
  76. struct work_struct ws;
  77. unsigned long cr1;
  78. wait_queue_head_t wait;
  79. spinlock_t lock;
  80. int width;
  81. };
  82. static void spi_sh_write(struct spi_sh_data *ss, unsigned long data,
  83. unsigned long offset)
  84. {
  85. if (ss->width == 8)
  86. iowrite8(data, ss->addr + (offset >> 2));
  87. else if (ss->width == 32)
  88. iowrite32(data, ss->addr + offset);
  89. }
  90. static unsigned long spi_sh_read(struct spi_sh_data *ss, unsigned long offset)
  91. {
  92. if (ss->width == 8)
  93. return ioread8(ss->addr + (offset >> 2));
  94. else if (ss->width == 32)
  95. return ioread32(ss->addr + offset);
  96. else
  97. return 0;
  98. }
  99. static void spi_sh_set_bit(struct spi_sh_data *ss, unsigned long val,
  100. unsigned long offset)
  101. {
  102. unsigned long tmp;
  103. tmp = spi_sh_read(ss, offset);
  104. tmp |= val;
  105. spi_sh_write(ss, tmp, offset);
  106. }
  107. static void spi_sh_clear_bit(struct spi_sh_data *ss, unsigned long val,
  108. unsigned long offset)
  109. {
  110. unsigned long tmp;
  111. tmp = spi_sh_read(ss, offset);
  112. tmp &= ~val;
  113. spi_sh_write(ss, tmp, offset);
  114. }
  115. static void clear_fifo(struct spi_sh_data *ss)
  116. {
  117. spi_sh_set_bit(ss, SPI_SH_RSTF, SPI_SH_CR2);
  118. spi_sh_clear_bit(ss, SPI_SH_RSTF, SPI_SH_CR2);
  119. }
  120. static int spi_sh_wait_receive_buffer(struct spi_sh_data *ss)
  121. {
  122. int timeout = 100000;
  123. while (spi_sh_read(ss, SPI_SH_CR1) & SPI_SH_RBE) {
  124. udelay(10);
  125. if (timeout-- < 0)
  126. return -ETIMEDOUT;
  127. }
  128. return 0;
  129. }
  130. static int spi_sh_wait_write_buffer_empty(struct spi_sh_data *ss)
  131. {
  132. int timeout = 100000;
  133. while (!(spi_sh_read(ss, SPI_SH_CR1) & SPI_SH_TBE)) {
  134. udelay(10);
  135. if (timeout-- < 0)
  136. return -ETIMEDOUT;
  137. }
  138. return 0;
  139. }
  140. static int spi_sh_send(struct spi_sh_data *ss, struct spi_message *mesg,
  141. struct spi_transfer *t)
  142. {
  143. int i, retval = 0;
  144. int remain = t->len;
  145. int cur_len;
  146. unsigned char *data;
  147. long ret;
  148. if (t->len)
  149. spi_sh_set_bit(ss, SPI_SH_SSA, SPI_SH_CR1);
  150. data = (unsigned char *)t->tx_buf;
  151. while (remain > 0) {
  152. cur_len = min(SPI_SH_FIFO_SIZE, remain);
  153. for (i = 0; i < cur_len &&
  154. !(spi_sh_read(ss, SPI_SH_CR4) &
  155. SPI_SH_WPABRT) &&
  156. !(spi_sh_read(ss, SPI_SH_CR1) & SPI_SH_TBF);
  157. i++)
  158. spi_sh_write(ss, (unsigned long)data[i], SPI_SH_TBR);
  159. if (spi_sh_read(ss, SPI_SH_CR4) & SPI_SH_WPABRT) {
  160. /* Abort SPI operation */
  161. spi_sh_set_bit(ss, SPI_SH_WPABRT, SPI_SH_CR4);
  162. retval = -EIO;
  163. break;
  164. }
  165. cur_len = i;
  166. remain -= cur_len;
  167. data += cur_len;
  168. if (remain > 0) {
  169. ss->cr1 &= ~SPI_SH_TBE;
  170. spi_sh_set_bit(ss, SPI_SH_TBE, SPI_SH_CR4);
  171. ret = wait_event_interruptible_timeout(ss->wait,
  172. ss->cr1 & SPI_SH_TBE,
  173. SPI_SH_SEND_TIMEOUT);
  174. if (ret == 0 && !(ss->cr1 & SPI_SH_TBE)) {
  175. printk(KERN_ERR "%s: timeout\n", __func__);
  176. return -ETIMEDOUT;
  177. }
  178. }
  179. }
  180. if (list_is_last(&t->transfer_list, &mesg->transfers)) {
  181. spi_sh_clear_bit(ss, SPI_SH_SSD | SPI_SH_SSDB, SPI_SH_CR1);
  182. spi_sh_set_bit(ss, SPI_SH_SSA, SPI_SH_CR1);
  183. ss->cr1 &= ~SPI_SH_TBE;
  184. spi_sh_set_bit(ss, SPI_SH_TBE, SPI_SH_CR4);
  185. ret = wait_event_interruptible_timeout(ss->wait,
  186. ss->cr1 & SPI_SH_TBE,
  187. SPI_SH_SEND_TIMEOUT);
  188. if (ret == 0 && (ss->cr1 & SPI_SH_TBE)) {
  189. printk(KERN_ERR "%s: timeout\n", __func__);
  190. return -ETIMEDOUT;
  191. }
  192. }
  193. return retval;
  194. }
  195. static int spi_sh_receive(struct spi_sh_data *ss, struct spi_message *mesg,
  196. struct spi_transfer *t)
  197. {
  198. int i;
  199. int remain = t->len;
  200. int cur_len;
  201. unsigned char *data;
  202. long ret;
  203. if (t->len > SPI_SH_MAX_BYTE)
  204. spi_sh_write(ss, SPI_SH_MAX_BYTE, SPI_SH_CR3);
  205. else
  206. spi_sh_write(ss, t->len, SPI_SH_CR3);
  207. spi_sh_clear_bit(ss, SPI_SH_SSD | SPI_SH_SSDB, SPI_SH_CR1);
  208. spi_sh_set_bit(ss, SPI_SH_SSA, SPI_SH_CR1);
  209. spi_sh_wait_write_buffer_empty(ss);
  210. data = (unsigned char *)t->rx_buf;
  211. while (remain > 0) {
  212. if (remain >= SPI_SH_FIFO_SIZE) {
  213. ss->cr1 &= ~SPI_SH_RBF;
  214. spi_sh_set_bit(ss, SPI_SH_RBF, SPI_SH_CR4);
  215. ret = wait_event_interruptible_timeout(ss->wait,
  216. ss->cr1 & SPI_SH_RBF,
  217. SPI_SH_RECEIVE_TIMEOUT);
  218. if (ret == 0 &&
  219. spi_sh_read(ss, SPI_SH_CR1) & SPI_SH_RBE) {
  220. printk(KERN_ERR "%s: timeout\n", __func__);
  221. return -ETIMEDOUT;
  222. }
  223. }
  224. cur_len = min(SPI_SH_FIFO_SIZE, remain);
  225. for (i = 0; i < cur_len; i++) {
  226. if (spi_sh_wait_receive_buffer(ss))
  227. break;
  228. data[i] = (unsigned char)spi_sh_read(ss, SPI_SH_RBR);
  229. }
  230. remain -= cur_len;
  231. data += cur_len;
  232. }
  233. /* deassert CS when SPI is receiving. */
  234. if (t->len > SPI_SH_MAX_BYTE) {
  235. clear_fifo(ss);
  236. spi_sh_write(ss, 1, SPI_SH_CR3);
  237. } else {
  238. spi_sh_write(ss, 0, SPI_SH_CR3);
  239. }
  240. return 0;
  241. }
  242. static void spi_sh_work(struct work_struct *work)
  243. {
  244. struct spi_sh_data *ss = container_of(work, struct spi_sh_data, ws);
  245. struct spi_message *mesg;
  246. struct spi_transfer *t;
  247. unsigned long flags;
  248. int ret;
  249. pr_debug("%s: enter\n", __func__);
  250. spin_lock_irqsave(&ss->lock, flags);
  251. while (!list_empty(&ss->queue)) {
  252. mesg = list_entry(ss->queue.next, struct spi_message, queue);
  253. list_del_init(&mesg->queue);
  254. spin_unlock_irqrestore(&ss->lock, flags);
  255. list_for_each_entry(t, &mesg->transfers, transfer_list) {
  256. pr_debug("tx_buf = %p, rx_buf = %p\n",
  257. t->tx_buf, t->rx_buf);
  258. pr_debug("len = %d, delay_usecs = %d\n",
  259. t->len, t->delay_usecs);
  260. if (t->tx_buf) {
  261. ret = spi_sh_send(ss, mesg, t);
  262. if (ret < 0)
  263. goto error;
  264. }
  265. if (t->rx_buf) {
  266. ret = spi_sh_receive(ss, mesg, t);
  267. if (ret < 0)
  268. goto error;
  269. }
  270. mesg->actual_length += t->len;
  271. }
  272. spin_lock_irqsave(&ss->lock, flags);
  273. mesg->status = 0;
  274. if (mesg->complete)
  275. mesg->complete(mesg->context);
  276. }
  277. clear_fifo(ss);
  278. spi_sh_set_bit(ss, SPI_SH_SSD, SPI_SH_CR1);
  279. udelay(100);
  280. spi_sh_clear_bit(ss, SPI_SH_SSA | SPI_SH_SSDB | SPI_SH_SSD,
  281. SPI_SH_CR1);
  282. clear_fifo(ss);
  283. spin_unlock_irqrestore(&ss->lock, flags);
  284. return;
  285. error:
  286. mesg->status = ret;
  287. if (mesg->complete)
  288. mesg->complete(mesg->context);
  289. spi_sh_clear_bit(ss, SPI_SH_SSA | SPI_SH_SSDB | SPI_SH_SSD,
  290. SPI_SH_CR1);
  291. clear_fifo(ss);
  292. }
  293. static int spi_sh_setup(struct spi_device *spi)
  294. {
  295. struct spi_sh_data *ss = spi_master_get_devdata(spi->master);
  296. pr_debug("%s: enter\n", __func__);
  297. spi_sh_write(ss, 0xfe, SPI_SH_CR1); /* SPI sycle stop */
  298. spi_sh_write(ss, 0x00, SPI_SH_CR1); /* CR1 init */
  299. spi_sh_write(ss, 0x00, SPI_SH_CR3); /* CR3 init */
  300. clear_fifo(ss);
  301. /* 1/8 clock */
  302. spi_sh_write(ss, spi_sh_read(ss, SPI_SH_CR2) | 0x07, SPI_SH_CR2);
  303. udelay(10);
  304. return 0;
  305. }
  306. static int spi_sh_transfer(struct spi_device *spi, struct spi_message *mesg)
  307. {
  308. struct spi_sh_data *ss = spi_master_get_devdata(spi->master);
  309. unsigned long flags;
  310. pr_debug("%s: enter\n", __func__);
  311. pr_debug("\tmode = %02x\n", spi->mode);
  312. spin_lock_irqsave(&ss->lock, flags);
  313. mesg->actual_length = 0;
  314. mesg->status = -EINPROGRESS;
  315. spi_sh_clear_bit(ss, SPI_SH_SSA, SPI_SH_CR1);
  316. list_add_tail(&mesg->queue, &ss->queue);
  317. queue_work(ss->workqueue, &ss->ws);
  318. spin_unlock_irqrestore(&ss->lock, flags);
  319. return 0;
  320. }
  321. static void spi_sh_cleanup(struct spi_device *spi)
  322. {
  323. struct spi_sh_data *ss = spi_master_get_devdata(spi->master);
  324. pr_debug("%s: enter\n", __func__);
  325. spi_sh_clear_bit(ss, SPI_SH_SSA | SPI_SH_SSDB | SPI_SH_SSD,
  326. SPI_SH_CR1);
  327. }
  328. static irqreturn_t spi_sh_irq(int irq, void *_ss)
  329. {
  330. struct spi_sh_data *ss = (struct spi_sh_data *)_ss;
  331. unsigned long cr1;
  332. cr1 = spi_sh_read(ss, SPI_SH_CR1);
  333. if (cr1 & SPI_SH_TBE)
  334. ss->cr1 |= SPI_SH_TBE;
  335. if (cr1 & SPI_SH_TBF)
  336. ss->cr1 |= SPI_SH_TBF;
  337. if (cr1 & SPI_SH_RBE)
  338. ss->cr1 |= SPI_SH_RBE;
  339. if (cr1 & SPI_SH_RBF)
  340. ss->cr1 |= SPI_SH_RBF;
  341. if (ss->cr1) {
  342. spi_sh_clear_bit(ss, ss->cr1, SPI_SH_CR4);
  343. wake_up(&ss->wait);
  344. }
  345. return IRQ_HANDLED;
  346. }
  347. static int spi_sh_remove(struct platform_device *pdev)
  348. {
  349. struct spi_sh_data *ss = platform_get_drvdata(pdev);
  350. spi_unregister_master(ss->master);
  351. destroy_workqueue(ss->workqueue);
  352. free_irq(ss->irq, ss);
  353. return 0;
  354. }
  355. static int spi_sh_probe(struct platform_device *pdev)
  356. {
  357. struct resource *res;
  358. struct spi_master *master;
  359. struct spi_sh_data *ss;
  360. int ret, irq;
  361. /* get base addr */
  362. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  363. if (unlikely(res == NULL)) {
  364. dev_err(&pdev->dev, "invalid resource\n");
  365. return -EINVAL;
  366. }
  367. irq = platform_get_irq(pdev, 0);
  368. if (irq < 0) {
  369. dev_err(&pdev->dev, "platform_get_irq error\n");
  370. return -ENODEV;
  371. }
  372. master = spi_alloc_master(&pdev->dev, sizeof(struct spi_sh_data));
  373. if (master == NULL) {
  374. dev_err(&pdev->dev, "spi_alloc_master error.\n");
  375. return -ENOMEM;
  376. }
  377. ss = spi_master_get_devdata(master);
  378. platform_set_drvdata(pdev, ss);
  379. switch (res->flags & IORESOURCE_MEM_TYPE_MASK) {
  380. case IORESOURCE_MEM_8BIT:
  381. ss->width = 8;
  382. break;
  383. case IORESOURCE_MEM_32BIT:
  384. ss->width = 32;
  385. break;
  386. default:
  387. dev_err(&pdev->dev, "No support width\n");
  388. ret = -ENODEV;
  389. goto error1;
  390. }
  391. ss->irq = irq;
  392. ss->master = master;
  393. ss->addr = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  394. if (ss->addr == NULL) {
  395. dev_err(&pdev->dev, "ioremap error.\n");
  396. ret = -ENOMEM;
  397. goto error1;
  398. }
  399. INIT_LIST_HEAD(&ss->queue);
  400. spin_lock_init(&ss->lock);
  401. INIT_WORK(&ss->ws, spi_sh_work);
  402. init_waitqueue_head(&ss->wait);
  403. ss->workqueue = create_singlethread_workqueue(
  404. dev_name(master->dev.parent));
  405. if (ss->workqueue == NULL) {
  406. dev_err(&pdev->dev, "create workqueue error\n");
  407. ret = -EBUSY;
  408. goto error1;
  409. }
  410. ret = request_irq(irq, spi_sh_irq, 0, "spi_sh", ss);
  411. if (ret < 0) {
  412. dev_err(&pdev->dev, "request_irq error\n");
  413. goto error2;
  414. }
  415. master->num_chipselect = 2;
  416. master->bus_num = pdev->id;
  417. master->setup = spi_sh_setup;
  418. master->transfer = spi_sh_transfer;
  419. master->cleanup = spi_sh_cleanup;
  420. ret = spi_register_master(master);
  421. if (ret < 0) {
  422. printk(KERN_ERR "spi_register_master error.\n");
  423. goto error3;
  424. }
  425. return 0;
  426. error3:
  427. free_irq(irq, ss);
  428. error2:
  429. destroy_workqueue(ss->workqueue);
  430. error1:
  431. spi_master_put(master);
  432. return ret;
  433. }
  434. static struct platform_driver spi_sh_driver = {
  435. .probe = spi_sh_probe,
  436. .remove = spi_sh_remove,
  437. .driver = {
  438. .name = "sh_spi",
  439. },
  440. };
  441. module_platform_driver(spi_sh_driver);
  442. MODULE_DESCRIPTION("SH SPI bus driver");
  443. MODULE_LICENSE("GPL");
  444. MODULE_AUTHOR("Yoshihiro Shimoda");
  445. MODULE_ALIAS("platform:sh_spi");