spi-omap-100k.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. #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 clk *ick;
  74. struct clk *fck;
  75. /* Virtual base address of the controller */
  76. void __iomem *base;
  77. };
  78. struct omap1_spi100k_cs {
  79. void __iomem *base;
  80. int word_len;
  81. };
  82. static void spi100k_enable_clock(struct spi_master *master)
  83. {
  84. unsigned int val;
  85. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  86. /* enable SPI */
  87. val = readw(spi100k->base + SPI_SETUP1);
  88. val |= SPI_SETUP1_CLOCK_ENABLE;
  89. writew(val, spi100k->base + SPI_SETUP1);
  90. }
  91. static void spi100k_disable_clock(struct spi_master *master)
  92. {
  93. unsigned int val;
  94. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  95. /* disable SPI */
  96. val = readw(spi100k->base + SPI_SETUP1);
  97. val &= ~SPI_SETUP1_CLOCK_ENABLE;
  98. writew(val, spi100k->base + SPI_SETUP1);
  99. }
  100. static void spi100k_write_data(struct spi_master *master, int len, int data)
  101. {
  102. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  103. /* write 16-bit word, shifting 8-bit data if necessary */
  104. if (len <= 8) {
  105. data <<= 8;
  106. len = 16;
  107. }
  108. spi100k_enable_clock(master);
  109. writew(data , spi100k->base + SPI_TX_MSB);
  110. writew(SPI_CTRL_SEN(0) |
  111. SPI_CTRL_WORD_SIZE(len) |
  112. SPI_CTRL_WR,
  113. spi100k->base + SPI_CTRL);
  114. /* Wait for bit ack send change */
  115. while ((readw(spi100k->base + SPI_STATUS) & SPI_STATUS_WE) != SPI_STATUS_WE)
  116. ;
  117. udelay(1000);
  118. spi100k_disable_clock(master);
  119. }
  120. static int spi100k_read_data(struct spi_master *master, int len)
  121. {
  122. int dataH, dataL;
  123. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  124. /* Always do at least 16 bits */
  125. if (len <= 8)
  126. len = 16;
  127. spi100k_enable_clock(master);
  128. writew(SPI_CTRL_SEN(0) |
  129. SPI_CTRL_WORD_SIZE(len) |
  130. SPI_CTRL_RD,
  131. spi100k->base + SPI_CTRL);
  132. while ((readw(spi100k->base + SPI_STATUS) & SPI_STATUS_RD) != SPI_STATUS_RD)
  133. ;
  134. udelay(1000);
  135. dataL = readw(spi100k->base + SPI_RX_LSB);
  136. dataH = readw(spi100k->base + SPI_RX_MSB);
  137. spi100k_disable_clock(master);
  138. return dataL;
  139. }
  140. static void spi100k_open(struct spi_master *master)
  141. {
  142. /* get control of SPI */
  143. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  144. writew(SPI_SETUP1_INT_READ_ENABLE |
  145. SPI_SETUP1_INT_WRITE_ENABLE |
  146. SPI_SETUP1_CLOCK_DIVISOR(0), spi100k->base + SPI_SETUP1);
  147. /* configure clock and interrupts */
  148. writew(SPI_SETUP2_ACTIVE_EDGE_FALLING |
  149. SPI_SETUP2_NEGATIVE_LEVEL |
  150. SPI_SETUP2_LEVEL_TRIGGER, spi100k->base + SPI_SETUP2);
  151. }
  152. static void omap1_spi100k_force_cs(struct omap1_spi100k *spi100k, int enable)
  153. {
  154. if (enable)
  155. writew(0x05fc, spi100k->base + SPI_CTRL);
  156. else
  157. writew(0x05fd, spi100k->base + SPI_CTRL);
  158. }
  159. static unsigned
  160. omap1_spi100k_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  161. {
  162. struct omap1_spi100k_cs *cs = spi->controller_state;
  163. unsigned int count, c;
  164. int word_len;
  165. count = xfer->len;
  166. c = count;
  167. word_len = cs->word_len;
  168. if (word_len <= 8) {
  169. u8 *rx;
  170. const u8 *tx;
  171. rx = xfer->rx_buf;
  172. tx = xfer->tx_buf;
  173. do {
  174. c -= 1;
  175. if (xfer->tx_buf != NULL)
  176. spi100k_write_data(spi->master, word_len, *tx++);
  177. if (xfer->rx_buf != NULL)
  178. *rx++ = spi100k_read_data(spi->master, word_len);
  179. } while (c);
  180. } else if (word_len <= 16) {
  181. u16 *rx;
  182. const u16 *tx;
  183. rx = xfer->rx_buf;
  184. tx = xfer->tx_buf;
  185. do {
  186. c -= 2;
  187. if (xfer->tx_buf != NULL)
  188. spi100k_write_data(spi->master, word_len, *tx++);
  189. if (xfer->rx_buf != NULL)
  190. *rx++ = spi100k_read_data(spi->master, word_len);
  191. } while (c);
  192. } else if (word_len <= 32) {
  193. u32 *rx;
  194. const u32 *tx;
  195. rx = xfer->rx_buf;
  196. tx = xfer->tx_buf;
  197. do {
  198. c -= 4;
  199. if (xfer->tx_buf != NULL)
  200. spi100k_write_data(spi->master, word_len, *tx);
  201. if (xfer->rx_buf != NULL)
  202. *rx = spi100k_read_data(spi->master, word_len);
  203. } while (c);
  204. }
  205. return count - c;
  206. }
  207. /* called only when no transfer is active to this device */
  208. static int omap1_spi100k_setup_transfer(struct spi_device *spi,
  209. struct spi_transfer *t)
  210. {
  211. struct omap1_spi100k *spi100k = spi_master_get_devdata(spi->master);
  212. struct omap1_spi100k_cs *cs = spi->controller_state;
  213. u8 word_len = spi->bits_per_word;
  214. if (t != NULL && t->bits_per_word)
  215. word_len = t->bits_per_word;
  216. if (!word_len)
  217. word_len = 8;
  218. if (spi->bits_per_word > 32)
  219. return -EINVAL;
  220. cs->word_len = word_len;
  221. /* SPI init before transfer */
  222. writew(0x3e , spi100k->base + SPI_SETUP1);
  223. writew(0x00 , spi100k->base + SPI_STATUS);
  224. writew(0x3e , spi100k->base + SPI_CTRL);
  225. return 0;
  226. }
  227. /* the spi->mode bits understood by this driver: */
  228. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
  229. static int omap1_spi100k_setup(struct spi_device *spi)
  230. {
  231. int ret;
  232. struct omap1_spi100k *spi100k;
  233. struct omap1_spi100k_cs *cs = spi->controller_state;
  234. spi100k = spi_master_get_devdata(spi->master);
  235. if (!cs) {
  236. cs = devm_kzalloc(&spi->dev, sizeof(*cs), GFP_KERNEL);
  237. if (!cs)
  238. return -ENOMEM;
  239. cs->base = spi100k->base + spi->chip_select * 0x14;
  240. spi->controller_state = cs;
  241. }
  242. spi100k_open(spi->master);
  243. clk_prepare_enable(spi100k->ick);
  244. clk_prepare_enable(spi100k->fck);
  245. ret = omap1_spi100k_setup_transfer(spi, NULL);
  246. clk_disable_unprepare(spi100k->ick);
  247. clk_disable_unprepare(spi100k->fck);
  248. return ret;
  249. }
  250. static int omap1_spi100k_prepare_hardware(struct spi_master *master)
  251. {
  252. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  253. clk_prepare_enable(spi100k->ick);
  254. clk_prepare_enable(spi100k->fck);
  255. return 0;
  256. }
  257. static int omap1_spi100k_transfer_one_message(struct spi_master *master,
  258. struct spi_message *m)
  259. {
  260. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  261. struct spi_device *spi = m->spi;
  262. struct spi_transfer *t = NULL;
  263. int cs_active = 0;
  264. int par_override = 0;
  265. int status = 0;
  266. list_for_each_entry(t, &m->transfers, transfer_list) {
  267. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  268. status = -EINVAL;
  269. break;
  270. }
  271. if (par_override || t->speed_hz || t->bits_per_word) {
  272. par_override = 1;
  273. status = omap1_spi100k_setup_transfer(spi, t);
  274. if (status < 0)
  275. break;
  276. if (!t->speed_hz && !t->bits_per_word)
  277. par_override = 0;
  278. }
  279. if (!cs_active) {
  280. omap1_spi100k_force_cs(spi100k, 1);
  281. cs_active = 1;
  282. }
  283. if (t->len) {
  284. unsigned count;
  285. count = omap1_spi100k_txrx_pio(spi, t);
  286. m->actual_length += count;
  287. if (count != t->len) {
  288. status = -EIO;
  289. break;
  290. }
  291. }
  292. if (t->delay_usecs)
  293. udelay(t->delay_usecs);
  294. /* ignore the "leave it on after last xfer" hint */
  295. if (t->cs_change) {
  296. omap1_spi100k_force_cs(spi100k, 0);
  297. cs_active = 0;
  298. }
  299. }
  300. /* Restore defaults if they were overriden */
  301. if (par_override) {
  302. par_override = 0;
  303. status = omap1_spi100k_setup_transfer(spi, NULL);
  304. }
  305. if (cs_active)
  306. omap1_spi100k_force_cs(spi100k, 0);
  307. m->status = status;
  308. spi_finalize_current_message(master);
  309. return status;
  310. }
  311. static int omap1_spi100k_unprepare_hardware(struct spi_master *master)
  312. {
  313. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  314. clk_disable_unprepare(spi100k->ick);
  315. clk_disable_unprepare(spi100k->fck);
  316. return 0;
  317. }
  318. static int omap1_spi100k_probe(struct platform_device *pdev)
  319. {
  320. struct spi_master *master;
  321. struct omap1_spi100k *spi100k;
  322. int status = 0;
  323. if (!pdev->id)
  324. return -EINVAL;
  325. master = spi_alloc_master(&pdev->dev, sizeof(*spi100k));
  326. if (master == NULL) {
  327. dev_dbg(&pdev->dev, "master allocation failed\n");
  328. return -ENOMEM;
  329. }
  330. if (pdev->id != -1)
  331. master->bus_num = pdev->id;
  332. master->setup = omap1_spi100k_setup;
  333. master->transfer_one_message = omap1_spi100k_transfer_one_message;
  334. master->prepare_transfer_hardware = omap1_spi100k_prepare_hardware;
  335. master->unprepare_transfer_hardware = omap1_spi100k_unprepare_hardware;
  336. master->cleanup = NULL;
  337. master->num_chipselect = 2;
  338. master->mode_bits = MODEBITS;
  339. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  340. master->min_speed_hz = OMAP1_SPI100K_MAX_FREQ/(1<<16);
  341. master->max_speed_hz = OMAP1_SPI100K_MAX_FREQ;
  342. platform_set_drvdata(pdev, master);
  343. spi100k = spi_master_get_devdata(master);
  344. /*
  345. * The memory region base address is taken as the platform_data.
  346. * You should allocate this with ioremap() before initializing
  347. * the SPI.
  348. */
  349. spi100k->base = (void __iomem *)dev_get_platdata(&pdev->dev);
  350. spi100k->ick = devm_clk_get(&pdev->dev, "ick");
  351. if (IS_ERR(spi100k->ick)) {
  352. dev_dbg(&pdev->dev, "can't get spi100k_ick\n");
  353. status = PTR_ERR(spi100k->ick);
  354. goto err;
  355. }
  356. spi100k->fck = devm_clk_get(&pdev->dev, "fck");
  357. if (IS_ERR(spi100k->fck)) {
  358. dev_dbg(&pdev->dev, "can't get spi100k_fck\n");
  359. status = PTR_ERR(spi100k->fck);
  360. goto err;
  361. }
  362. status = devm_spi_register_master(&pdev->dev, master);
  363. if (status < 0)
  364. goto err;
  365. return status;
  366. err:
  367. spi_master_put(master);
  368. return status;
  369. }
  370. static struct platform_driver omap1_spi100k_driver = {
  371. .driver = {
  372. .name = "omap1_spi100k",
  373. .owner = THIS_MODULE,
  374. },
  375. .probe = omap1_spi100k_probe,
  376. };
  377. module_platform_driver(omap1_spi100k_driver);
  378. MODULE_DESCRIPTION("OMAP7xx SPI 100k controller driver");
  379. MODULE_AUTHOR("Fabrice Crohas <fcrohas@gmail.com>");
  380. MODULE_LICENSE("GPL");