omap_spi_100k.c 16 KB

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