omap_spi_100k.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /*
  2. * OMAP7xx SPI 100k controller driver
  3. * Author: Fabrice Crohas <fcrohas@gmail.com>
  4. * from original omap1_mcspi driver
  5. *
  6. * Copyright (C) 2005, 2006 Nokia Corporation
  7. * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
  8. * Juha Yrj�l� <juha.yrjola@nokia.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/init.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/module.h>
  29. #include <linux/device.h>
  30. #include <linux/delay.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/err.h>
  33. #include <linux/clk.h>
  34. #include <linux/io.h>
  35. #include <linux/gpio.h>
  36. #include <linux/slab.h>
  37. #include <linux/spi/spi.h>
  38. #include <plat/clock.h>
  39. #define OMAP1_SPI100K_MAX_FREQ 48000000
  40. #define ICR_SPITAS (OMAP7XX_ICR_BASE + 0x12)
  41. #define SPI_SETUP1 0x00
  42. #define SPI_SETUP2 0x02
  43. #define SPI_CTRL 0x04
  44. #define SPI_STATUS 0x06
  45. #define SPI_TX_LSB 0x08
  46. #define SPI_TX_MSB 0x0a
  47. #define SPI_RX_LSB 0x0c
  48. #define SPI_RX_MSB 0x0e
  49. #define SPI_SETUP1_INT_READ_ENABLE (1UL << 5)
  50. #define SPI_SETUP1_INT_WRITE_ENABLE (1UL << 4)
  51. #define SPI_SETUP1_CLOCK_DIVISOR(x) ((x) << 1)
  52. #define SPI_SETUP1_CLOCK_ENABLE (1UL << 0)
  53. #define SPI_SETUP2_ACTIVE_EDGE_FALLING (0UL << 0)
  54. #define SPI_SETUP2_ACTIVE_EDGE_RISING (1UL << 0)
  55. #define SPI_SETUP2_NEGATIVE_LEVEL (0UL << 5)
  56. #define SPI_SETUP2_POSITIVE_LEVEL (1UL << 5)
  57. #define SPI_SETUP2_LEVEL_TRIGGER (0UL << 10)
  58. #define SPI_SETUP2_EDGE_TRIGGER (1UL << 10)
  59. #define SPI_CTRL_SEN(x) ((x) << 7)
  60. #define SPI_CTRL_WORD_SIZE(x) (((x) - 1) << 2)
  61. #define SPI_CTRL_WR (1UL << 1)
  62. #define SPI_CTRL_RD (1UL << 0)
  63. #define SPI_STATUS_WE (1UL << 1)
  64. #define SPI_STATUS_RD (1UL << 0)
  65. #define WRITE 0
  66. #define READ 1
  67. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  68. * cache operations; better heuristics consider wordsize and bitrate.
  69. */
  70. #define DMA_MIN_BYTES 8
  71. #define SPI_RUNNING 0
  72. #define SPI_SHUTDOWN 1
  73. struct omap1_spi100k {
  74. struct work_struct work;
  75. /* lock protects queue and registers */
  76. spinlock_t lock;
  77. struct list_head msg_queue;
  78. struct spi_master *master;
  79. struct clk *ick;
  80. struct clk *fck;
  81. /* Virtual base address of the controller */
  82. void __iomem *base;
  83. /* State of the SPI */
  84. unsigned int state;
  85. };
  86. struct omap1_spi100k_cs {
  87. void __iomem *base;
  88. int word_len;
  89. };
  90. static struct workqueue_struct *omap1_spi100k_wq;
  91. #define MOD_REG_BIT(val, mask, set) do { \
  92. if (set) \
  93. val |= mask; \
  94. else \
  95. val &= ~mask; \
  96. } while (0)
  97. static void spi100k_enable_clock(struct spi_master *master)
  98. {
  99. unsigned int val;
  100. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  101. /* enable SPI */
  102. val = readw(spi100k->base + SPI_SETUP1);
  103. val |= SPI_SETUP1_CLOCK_ENABLE;
  104. writew(val, spi100k->base + SPI_SETUP1);
  105. }
  106. static void spi100k_disable_clock(struct spi_master *master)
  107. {
  108. unsigned int val;
  109. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  110. /* disable SPI */
  111. val = readw(spi100k->base + SPI_SETUP1);
  112. val &= ~SPI_SETUP1_CLOCK_ENABLE;
  113. writew(val, spi100k->base + SPI_SETUP1);
  114. }
  115. static void spi100k_write_data(struct spi_master *master, int len, int data)
  116. {
  117. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  118. /* write 16-bit word */
  119. spi100k_enable_clock(master);
  120. writew( data , spi100k->base + SPI_TX_MSB);
  121. writew(SPI_CTRL_SEN(0) |
  122. SPI_CTRL_WORD_SIZE(len) |
  123. SPI_CTRL_WR,
  124. spi100k->base + SPI_CTRL);
  125. /* Wait for bit ack send change */
  126. while((readw(spi100k->base + SPI_STATUS) & SPI_STATUS_WE) != SPI_STATUS_WE);
  127. udelay(1000);
  128. spi100k_disable_clock(master);
  129. }
  130. static int spi100k_read_data(struct spi_master *master, int len)
  131. {
  132. int dataH,dataL;
  133. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  134. spi100k_enable_clock(master);
  135. writew(SPI_CTRL_SEN(0) |
  136. SPI_CTRL_WORD_SIZE(len) |
  137. SPI_CTRL_RD,
  138. spi100k->base + SPI_CTRL);
  139. while((readw(spi100k->base + SPI_STATUS) & SPI_STATUS_RD) != SPI_STATUS_RD);
  140. udelay(1000);
  141. dataL = readw(spi100k->base + SPI_RX_LSB);
  142. dataH = readw(spi100k->base + SPI_RX_MSB);
  143. spi100k_disable_clock(master);
  144. return dataL;
  145. }
  146. static void spi100k_open(struct spi_master *master)
  147. {
  148. /* get control of SPI */
  149. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  150. writew(SPI_SETUP1_INT_READ_ENABLE |
  151. SPI_SETUP1_INT_WRITE_ENABLE |
  152. SPI_SETUP1_CLOCK_DIVISOR(0), spi100k->base + SPI_SETUP1);
  153. /* configure clock and interrupts */
  154. writew(SPI_SETUP2_ACTIVE_EDGE_FALLING |
  155. SPI_SETUP2_NEGATIVE_LEVEL |
  156. SPI_SETUP2_LEVEL_TRIGGER, spi100k->base + SPI_SETUP2);
  157. }
  158. static void omap1_spi100k_force_cs(struct omap1_spi100k *spi100k, int enable)
  159. {
  160. if (enable)
  161. writew(0x05fc, spi100k->base + SPI_CTRL);
  162. else
  163. writew(0x05fd, spi100k->base + SPI_CTRL);
  164. }
  165. static unsigned
  166. omap1_spi100k_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  167. {
  168. struct omap1_spi100k *spi100k;
  169. struct omap1_spi100k_cs *cs = spi->controller_state;
  170. unsigned int count, c;
  171. int word_len;
  172. spi100k = spi_master_get_devdata(spi->master);
  173. count = xfer->len;
  174. c = count;
  175. word_len = cs->word_len;
  176. /* RX_ONLY mode needs dummy data in TX reg */
  177. if (xfer->tx_buf == NULL)
  178. spi100k_write_data(spi->master,word_len, 0);
  179. if (word_len <= 8) {
  180. u8 *rx;
  181. const u8 *tx;
  182. rx = xfer->rx_buf;
  183. tx = xfer->tx_buf;
  184. do {
  185. c-=1;
  186. if (xfer->tx_buf != NULL)
  187. spi100k_write_data(spi->master,word_len, *tx);
  188. if (xfer->rx_buf != NULL)
  189. *rx = spi100k_read_data(spi->master,word_len);
  190. } while(c);
  191. } else if (word_len <= 16) {
  192. u16 *rx;
  193. const u16 *tx;
  194. rx = xfer->rx_buf;
  195. tx = xfer->tx_buf;
  196. do {
  197. c-=2;
  198. if (xfer->tx_buf != NULL)
  199. spi100k_write_data(spi->master,word_len, *tx++);
  200. if (xfer->rx_buf != NULL)
  201. *rx++ = spi100k_read_data(spi->master,word_len);
  202. } while(c);
  203. } else if (word_len <= 32) {
  204. u32 *rx;
  205. const u32 *tx;
  206. rx = xfer->rx_buf;
  207. tx = xfer->tx_buf;
  208. do {
  209. c-=4;
  210. if (xfer->tx_buf != NULL)
  211. spi100k_write_data(spi->master,word_len, *tx);
  212. if (xfer->rx_buf != NULL)
  213. *rx = spi100k_read_data(spi->master,word_len);
  214. } while(c);
  215. }
  216. return count - c;
  217. }
  218. /* called only when no transfer is active to this device */
  219. static int omap1_spi100k_setup_transfer(struct spi_device *spi,
  220. struct spi_transfer *t)
  221. {
  222. struct omap1_spi100k *spi100k = spi_master_get_devdata(spi->master);
  223. struct omap1_spi100k_cs *cs = spi->controller_state;
  224. u8 word_len = spi->bits_per_word;
  225. if (t != NULL && t->bits_per_word)
  226. word_len = t->bits_per_word;
  227. if (!word_len)
  228. word_len = 8;
  229. if (spi->bits_per_word > 32)
  230. return -EINVAL;
  231. cs->word_len = word_len;
  232. /* SPI init before transfer */
  233. writew(0x3e , spi100k->base + SPI_SETUP1);
  234. writew(0x00 , spi100k->base + SPI_STATUS);
  235. writew(0x3e , spi100k->base + SPI_CTRL);
  236. return 0;
  237. }
  238. /* the spi->mode bits understood by this driver: */
  239. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
  240. static int omap1_spi100k_setup(struct spi_device *spi)
  241. {
  242. int ret;
  243. struct omap1_spi100k *spi100k;
  244. struct omap1_spi100k_cs *cs = spi->controller_state;
  245. if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
  246. dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
  247. spi->bits_per_word);
  248. return -EINVAL;
  249. }
  250. spi100k = spi_master_get_devdata(spi->master);
  251. if (!cs) {
  252. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  253. if (!cs)
  254. return -ENOMEM;
  255. cs->base = spi100k->base + spi->chip_select * 0x14;
  256. spi->controller_state = cs;
  257. }
  258. spi100k_open(spi->master);
  259. clk_enable(spi100k->ick);
  260. clk_enable(spi100k->fck);
  261. ret = omap1_spi100k_setup_transfer(spi, NULL);
  262. clk_disable(spi100k->ick);
  263. clk_disable(spi100k->fck);
  264. return ret;
  265. }
  266. static void omap1_spi100k_work(struct work_struct *work)
  267. {
  268. struct omap1_spi100k *spi100k;
  269. int status = 0;
  270. spi100k = container_of(work, struct omap1_spi100k, work);
  271. spin_lock_irq(&spi100k->lock);
  272. clk_enable(spi100k->ick);
  273. clk_enable(spi100k->fck);
  274. /* We only enable one channel at a time -- the one whose message is
  275. * at the head of the queue -- although this controller would gladly
  276. * arbitrate among multiple channels. This corresponds to "single
  277. * channel" master mode. As a side effect, we need to manage the
  278. * chipselect with the FORCE bit ... CS != channel enable.
  279. */
  280. while (!list_empty(&spi100k->msg_queue)) {
  281. struct spi_message *m;
  282. struct spi_device *spi;
  283. struct spi_transfer *t = NULL;
  284. int cs_active = 0;
  285. struct omap1_spi100k_cs *cs;
  286. int par_override = 0;
  287. m = container_of(spi100k->msg_queue.next, struct spi_message,
  288. queue);
  289. list_del_init(&m->queue);
  290. spin_unlock_irq(&spi100k->lock);
  291. spi = m->spi;
  292. cs = spi->controller_state;
  293. list_for_each_entry(t, &m->transfers, transfer_list) {
  294. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  295. status = -EINVAL;
  296. break;
  297. }
  298. if (par_override || t->speed_hz || t->bits_per_word) {
  299. par_override = 1;
  300. status = omap1_spi100k_setup_transfer(spi, t);
  301. if (status < 0)
  302. break;
  303. if (!t->speed_hz && !t->bits_per_word)
  304. par_override = 0;
  305. }
  306. if (!cs_active) {
  307. omap1_spi100k_force_cs(spi100k, 1);
  308. cs_active = 1;
  309. }
  310. if (t->len) {
  311. unsigned count;
  312. /* RX_ONLY mode needs dummy data in TX reg */
  313. if (t->tx_buf == NULL)
  314. spi100k_write_data(spi->master, 8, 0);
  315. count = omap1_spi100k_txrx_pio(spi, t);
  316. m->actual_length += count;
  317. if (count != t->len) {
  318. status = -EIO;
  319. break;
  320. }
  321. }
  322. if (t->delay_usecs)
  323. udelay(t->delay_usecs);
  324. /* ignore the "leave it on after last xfer" hint */
  325. if (t->cs_change) {
  326. omap1_spi100k_force_cs(spi100k, 0);
  327. cs_active = 0;
  328. }
  329. }
  330. /* Restore defaults if they were overriden */
  331. if (par_override) {
  332. par_override = 0;
  333. status = omap1_spi100k_setup_transfer(spi, NULL);
  334. }
  335. if (cs_active)
  336. omap1_spi100k_force_cs(spi100k, 0);
  337. m->status = status;
  338. m->complete(m->context);
  339. spin_lock_irq(&spi100k->lock);
  340. }
  341. clk_disable(spi100k->ick);
  342. clk_disable(spi100k->fck);
  343. spin_unlock_irq(&spi100k->lock);
  344. if (status < 0)
  345. printk(KERN_WARNING "spi transfer failed with %d\n", status);
  346. }
  347. static int omap1_spi100k_transfer(struct spi_device *spi, struct spi_message *m)
  348. {
  349. struct omap1_spi100k *spi100k;
  350. unsigned long flags;
  351. struct spi_transfer *t;
  352. m->actual_length = 0;
  353. m->status = -EINPROGRESS;
  354. spi100k = spi_master_get_devdata(spi->master);
  355. /* Don't accept new work if we're shutting down */
  356. if (spi100k->state == SPI_SHUTDOWN)
  357. return -ESHUTDOWN;
  358. /* reject invalid messages and transfers */
  359. if (list_empty(&m->transfers) || !m->complete)
  360. return -EINVAL;
  361. list_for_each_entry(t, &m->transfers, transfer_list) {
  362. const void *tx_buf = t->tx_buf;
  363. void *rx_buf = t->rx_buf;
  364. unsigned len = t->len;
  365. if (t->speed_hz > OMAP1_SPI100K_MAX_FREQ
  366. || (len && !(rx_buf || tx_buf))
  367. || (t->bits_per_word &&
  368. ( t->bits_per_word < 4
  369. || t->bits_per_word > 32))) {
  370. dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  371. t->speed_hz,
  372. len,
  373. tx_buf ? "tx" : "",
  374. rx_buf ? "rx" : "",
  375. t->bits_per_word);
  376. return -EINVAL;
  377. }
  378. if (t->speed_hz && t->speed_hz < OMAP1_SPI100K_MAX_FREQ/(1<<16)) {
  379. dev_dbg(&spi->dev, "%d Hz max exceeds %d\n",
  380. t->speed_hz,
  381. OMAP1_SPI100K_MAX_FREQ/(1<<16));
  382. return -EINVAL;
  383. }
  384. }
  385. spin_lock_irqsave(&spi100k->lock, flags);
  386. list_add_tail(&m->queue, &spi100k->msg_queue);
  387. queue_work(omap1_spi100k_wq, &spi100k->work);
  388. spin_unlock_irqrestore(&spi100k->lock, flags);
  389. return 0;
  390. }
  391. static int __init omap1_spi100k_reset(struct omap1_spi100k *spi100k)
  392. {
  393. return 0;
  394. }
  395. static int __devinit omap1_spi100k_probe(struct platform_device *pdev)
  396. {
  397. struct spi_master *master;
  398. struct omap1_spi100k *spi100k;
  399. int status = 0;
  400. if (!pdev->id)
  401. return -EINVAL;
  402. master = spi_alloc_master(&pdev->dev, sizeof *spi100k);
  403. if (master == NULL) {
  404. dev_dbg(&pdev->dev, "master allocation failed\n");
  405. return -ENOMEM;
  406. }
  407. if (pdev->id != -1)
  408. master->bus_num = pdev->id;
  409. master->setup = omap1_spi100k_setup;
  410. master->transfer = omap1_spi100k_transfer;
  411. master->cleanup = NULL;
  412. master->num_chipselect = 2;
  413. master->mode_bits = MODEBITS;
  414. dev_set_drvdata(&pdev->dev, master);
  415. spi100k = spi_master_get_devdata(master);
  416. spi100k->master = master;
  417. /*
  418. * The memory region base address is taken as the platform_data.
  419. * You should allocate this with ioremap() before initializing
  420. * the SPI.
  421. */
  422. spi100k->base = (void __iomem *) pdev->dev.platform_data;
  423. INIT_WORK(&spi100k->work, omap1_spi100k_work);
  424. spin_lock_init(&spi100k->lock);
  425. INIT_LIST_HEAD(&spi100k->msg_queue);
  426. spi100k->ick = clk_get(&pdev->dev, "ick");
  427. if (IS_ERR(spi100k->ick)) {
  428. dev_dbg(&pdev->dev, "can't get spi100k_ick\n");
  429. status = PTR_ERR(spi100k->ick);
  430. goto err1;
  431. }
  432. spi100k->fck = clk_get(&pdev->dev, "fck");
  433. if (IS_ERR(spi100k->fck)) {
  434. dev_dbg(&pdev->dev, "can't get spi100k_fck\n");
  435. status = PTR_ERR(spi100k->fck);
  436. goto err2;
  437. }
  438. if (omap1_spi100k_reset(spi100k) < 0)
  439. goto err3;
  440. status = spi_register_master(master);
  441. if (status < 0)
  442. goto err3;
  443. spi100k->state = SPI_RUNNING;
  444. return status;
  445. err3:
  446. clk_put(spi100k->fck);
  447. err2:
  448. clk_put(spi100k->ick);
  449. err1:
  450. spi_master_put(master);
  451. return status;
  452. }
  453. static int __exit omap1_spi100k_remove(struct platform_device *pdev)
  454. {
  455. struct spi_master *master;
  456. struct omap1_spi100k *spi100k;
  457. struct resource *r;
  458. unsigned limit = 500;
  459. unsigned long flags;
  460. int status = 0;
  461. master = dev_get_drvdata(&pdev->dev);
  462. spi100k = spi_master_get_devdata(master);
  463. spin_lock_irqsave(&spi100k->lock, flags);
  464. spi100k->state = SPI_SHUTDOWN;
  465. while (!list_empty(&spi100k->msg_queue) && limit--) {
  466. spin_unlock_irqrestore(&spi100k->lock, flags);
  467. msleep(10);
  468. spin_lock_irqsave(&spi100k->lock, flags);
  469. }
  470. if (!list_empty(&spi100k->msg_queue))
  471. status = -EBUSY;
  472. spin_unlock_irqrestore(&spi100k->lock, flags);
  473. if (status != 0)
  474. return status;
  475. clk_put(spi100k->fck);
  476. clk_put(spi100k->ick);
  477. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  478. spi_unregister_master(master);
  479. return 0;
  480. }
  481. static struct platform_driver omap1_spi100k_driver = {
  482. .driver = {
  483. .name = "omap1_spi100k",
  484. .owner = THIS_MODULE,
  485. },
  486. .remove = __exit_p(omap1_spi100k_remove),
  487. };
  488. static int __init omap1_spi100k_init(void)
  489. {
  490. omap1_spi100k_wq = create_singlethread_workqueue(
  491. omap1_spi100k_driver.driver.name);
  492. if (omap1_spi100k_wq == NULL)
  493. return -1;
  494. return platform_driver_probe(&omap1_spi100k_driver, omap1_spi100k_probe);
  495. }
  496. static void __exit omap1_spi100k_exit(void)
  497. {
  498. platform_driver_unregister(&omap1_spi100k_driver);
  499. destroy_workqueue(omap1_spi100k_wq);
  500. }
  501. module_init(omap1_spi100k_init);
  502. module_exit(omap1_spi100k_exit);
  503. MODULE_DESCRIPTION("OMAP7xx SPI 100k controller driver");
  504. MODULE_AUTHOR("Fabrice Crohas <fcrohas@gmail.com>");
  505. MODULE_LICENSE("GPL");