omap2_mcspi.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. /*
  2. * OMAP2 McSPI controller driver
  3. *
  4. * Copyright (C) 2005, 2006 Nokia Corporation
  5. * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
  6. * Juha Yrjölä <juha.yrjola@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  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. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/module.h>
  27. #include <linux/device.h>
  28. #include <linux/delay.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/err.h>
  32. #include <linux/clk.h>
  33. #include <linux/io.h>
  34. #include <linux/spi/spi.h>
  35. #include <asm/arch/dma.h>
  36. #include <asm/arch/clock.h>
  37. #define OMAP2_MCSPI_MAX_FREQ 48000000
  38. #define OMAP2_MCSPI_REVISION 0x00
  39. #define OMAP2_MCSPI_SYSCONFIG 0x10
  40. #define OMAP2_MCSPI_SYSSTATUS 0x14
  41. #define OMAP2_MCSPI_IRQSTATUS 0x18
  42. #define OMAP2_MCSPI_IRQENABLE 0x1c
  43. #define OMAP2_MCSPI_WAKEUPENABLE 0x20
  44. #define OMAP2_MCSPI_SYST 0x24
  45. #define OMAP2_MCSPI_MODULCTRL 0x28
  46. /* per-channel banks, 0x14 bytes each, first is: */
  47. #define OMAP2_MCSPI_CHCONF0 0x2c
  48. #define OMAP2_MCSPI_CHSTAT0 0x30
  49. #define OMAP2_MCSPI_CHCTRL0 0x34
  50. #define OMAP2_MCSPI_TX0 0x38
  51. #define OMAP2_MCSPI_RX0 0x3c
  52. /* per-register bitmasks: */
  53. #define OMAP2_MCSPI_SYSCONFIG_AUTOIDLE (1 << 0)
  54. #define OMAP2_MCSPI_SYSCONFIG_SOFTRESET (1 << 1)
  55. #define OMAP2_MCSPI_SYSSTATUS_RESETDONE (1 << 0)
  56. #define OMAP2_MCSPI_MODULCTRL_SINGLE (1 << 0)
  57. #define OMAP2_MCSPI_MODULCTRL_MS (1 << 2)
  58. #define OMAP2_MCSPI_MODULCTRL_STEST (1 << 3)
  59. #define OMAP2_MCSPI_CHCONF_PHA (1 << 0)
  60. #define OMAP2_MCSPI_CHCONF_POL (1 << 1)
  61. #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
  62. #define OMAP2_MCSPI_CHCONF_EPOL (1 << 6)
  63. #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
  64. #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY (0x01 << 12)
  65. #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY (0x02 << 12)
  66. #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
  67. #define OMAP2_MCSPI_CHCONF_DMAW (1 << 14)
  68. #define OMAP2_MCSPI_CHCONF_DMAR (1 << 15)
  69. #define OMAP2_MCSPI_CHCONF_DPE0 (1 << 16)
  70. #define OMAP2_MCSPI_CHCONF_DPE1 (1 << 17)
  71. #define OMAP2_MCSPI_CHCONF_IS (1 << 18)
  72. #define OMAP2_MCSPI_CHCONF_TURBO (1 << 19)
  73. #define OMAP2_MCSPI_CHCONF_FORCE (1 << 20)
  74. #define OMAP2_MCSPI_CHSTAT_RXS (1 << 0)
  75. #define OMAP2_MCSPI_CHSTAT_TXS (1 << 1)
  76. #define OMAP2_MCSPI_CHSTAT_EOT (1 << 2)
  77. #define OMAP2_MCSPI_CHCTRL_EN (1 << 0)
  78. /* We have 2 DMA channels per CS, one for RX and one for TX */
  79. struct omap2_mcspi_dma {
  80. int dma_tx_channel;
  81. int dma_rx_channel;
  82. int dma_tx_sync_dev;
  83. int dma_rx_sync_dev;
  84. struct completion dma_tx_completion;
  85. struct completion dma_rx_completion;
  86. };
  87. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  88. * cache operations; better heuristics consider wordsize and bitrate.
  89. */
  90. #define DMA_MIN_BYTES 8
  91. struct omap2_mcspi {
  92. struct work_struct work;
  93. /* lock protects queue and registers */
  94. spinlock_t lock;
  95. struct list_head msg_queue;
  96. struct spi_master *master;
  97. struct clk *ick;
  98. struct clk *fck;
  99. /* Virtual base address of the controller */
  100. void __iomem *base;
  101. /* SPI1 has 4 channels, while SPI2 has 2 */
  102. struct omap2_mcspi_dma *dma_channels;
  103. };
  104. struct omap2_mcspi_cs {
  105. void __iomem *base;
  106. int word_len;
  107. };
  108. static struct workqueue_struct *omap2_mcspi_wq;
  109. #define MOD_REG_BIT(val, mask, set) do { \
  110. if (set) \
  111. val |= mask; \
  112. else \
  113. val &= ~mask; \
  114. } while (0)
  115. static inline void mcspi_write_reg(struct spi_master *master,
  116. int idx, u32 val)
  117. {
  118. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  119. __raw_writel(val, mcspi->base + idx);
  120. }
  121. static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
  122. {
  123. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  124. return __raw_readl(mcspi->base + idx);
  125. }
  126. static inline void mcspi_write_cs_reg(const struct spi_device *spi,
  127. int idx, u32 val)
  128. {
  129. struct omap2_mcspi_cs *cs = spi->controller_state;
  130. __raw_writel(val, cs->base + idx);
  131. }
  132. static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
  133. {
  134. struct omap2_mcspi_cs *cs = spi->controller_state;
  135. return __raw_readl(cs->base + idx);
  136. }
  137. static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
  138. int is_read, int enable)
  139. {
  140. u32 l, rw;
  141. l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  142. if (is_read) /* 1 is read, 0 write */
  143. rw = OMAP2_MCSPI_CHCONF_DMAR;
  144. else
  145. rw = OMAP2_MCSPI_CHCONF_DMAW;
  146. MOD_REG_BIT(l, rw, enable);
  147. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, l);
  148. }
  149. static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
  150. {
  151. u32 l;
  152. l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
  153. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
  154. }
  155. static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
  156. {
  157. u32 l;
  158. l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  159. MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
  160. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, l);
  161. }
  162. static void omap2_mcspi_set_master_mode(struct spi_master *master)
  163. {
  164. u32 l;
  165. /* setup when switching from (reset default) slave mode
  166. * to single-channel master mode
  167. */
  168. l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
  169. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0);
  170. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0);
  171. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1);
  172. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
  173. }
  174. static unsigned
  175. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  176. {
  177. struct omap2_mcspi *mcspi;
  178. struct omap2_mcspi_cs *cs = spi->controller_state;
  179. struct omap2_mcspi_dma *mcspi_dma;
  180. unsigned int count, c;
  181. unsigned long base, tx_reg, rx_reg;
  182. int word_len, data_type, element_count;
  183. u8 * rx;
  184. const u8 * tx;
  185. mcspi = spi_master_get_devdata(spi->master);
  186. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  187. count = xfer->len;
  188. c = count;
  189. word_len = cs->word_len;
  190. base = (unsigned long) io_v2p(cs->base);
  191. tx_reg = base + OMAP2_MCSPI_TX0;
  192. rx_reg = base + OMAP2_MCSPI_RX0;
  193. rx = xfer->rx_buf;
  194. tx = xfer->tx_buf;
  195. if (word_len <= 8) {
  196. data_type = OMAP_DMA_DATA_TYPE_S8;
  197. element_count = count;
  198. } else if (word_len <= 16) {
  199. data_type = OMAP_DMA_DATA_TYPE_S16;
  200. element_count = count >> 1;
  201. } else /* word_len <= 32 */ {
  202. data_type = OMAP_DMA_DATA_TYPE_S32;
  203. element_count = count >> 2;
  204. }
  205. if (tx != NULL) {
  206. omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel,
  207. data_type, element_count, 1,
  208. OMAP_DMA_SYNC_ELEMENT,
  209. mcspi_dma->dma_tx_sync_dev, 0);
  210. omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0,
  211. OMAP_DMA_AMODE_CONSTANT,
  212. tx_reg, 0, 0);
  213. omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0,
  214. OMAP_DMA_AMODE_POST_INC,
  215. xfer->tx_dma, 0, 0);
  216. }
  217. if (rx != NULL) {
  218. omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel,
  219. data_type, element_count, 1,
  220. OMAP_DMA_SYNC_ELEMENT,
  221. mcspi_dma->dma_rx_sync_dev, 1);
  222. omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0,
  223. OMAP_DMA_AMODE_CONSTANT,
  224. rx_reg, 0, 0);
  225. omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0,
  226. OMAP_DMA_AMODE_POST_INC,
  227. xfer->rx_dma, 0, 0);
  228. }
  229. if (tx != NULL) {
  230. omap_start_dma(mcspi_dma->dma_tx_channel);
  231. omap2_mcspi_set_dma_req(spi, 0, 1);
  232. }
  233. if (rx != NULL) {
  234. omap_start_dma(mcspi_dma->dma_rx_channel);
  235. omap2_mcspi_set_dma_req(spi, 1, 1);
  236. }
  237. if (tx != NULL) {
  238. wait_for_completion(&mcspi_dma->dma_tx_completion);
  239. dma_unmap_single(NULL, xfer->tx_dma, count, DMA_TO_DEVICE);
  240. }
  241. if (rx != NULL) {
  242. wait_for_completion(&mcspi_dma->dma_rx_completion);
  243. dma_unmap_single(NULL, xfer->rx_dma, count, DMA_FROM_DEVICE);
  244. }
  245. return count;
  246. }
  247. static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
  248. {
  249. unsigned long timeout;
  250. timeout = jiffies + msecs_to_jiffies(1000);
  251. while (!(__raw_readl(reg) & bit)) {
  252. if (time_after(jiffies, timeout))
  253. return -1;
  254. cpu_relax();
  255. }
  256. return 0;
  257. }
  258. static unsigned
  259. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  260. {
  261. struct omap2_mcspi *mcspi;
  262. struct omap2_mcspi_cs *cs = spi->controller_state;
  263. unsigned int count, c;
  264. u32 l;
  265. void __iomem *base = cs->base;
  266. void __iomem *tx_reg;
  267. void __iomem *rx_reg;
  268. void __iomem *chstat_reg;
  269. int word_len;
  270. mcspi = spi_master_get_devdata(spi->master);
  271. count = xfer->len;
  272. c = count;
  273. word_len = cs->word_len;
  274. l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  275. l &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  276. /* We store the pre-calculated register addresses on stack to speed
  277. * up the transfer loop. */
  278. tx_reg = base + OMAP2_MCSPI_TX0;
  279. rx_reg = base + OMAP2_MCSPI_RX0;
  280. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  281. if (word_len <= 8) {
  282. u8 *rx;
  283. const u8 *tx;
  284. rx = xfer->rx_buf;
  285. tx = xfer->tx_buf;
  286. do {
  287. if (tx != NULL) {
  288. if (mcspi_wait_for_reg_bit(chstat_reg,
  289. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  290. dev_err(&spi->dev, "TXS timed out\n");
  291. goto out;
  292. }
  293. #ifdef VERBOSE
  294. dev_dbg(&spi->dev, "write-%d %02x\n",
  295. word_len, *tx);
  296. #endif
  297. __raw_writel(*tx++, tx_reg);
  298. }
  299. if (rx != NULL) {
  300. if (mcspi_wait_for_reg_bit(chstat_reg,
  301. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  302. dev_err(&spi->dev, "RXS timed out\n");
  303. goto out;
  304. }
  305. /* prevent last RX_ONLY read from triggering
  306. * more word i/o: switch to rx+tx
  307. */
  308. if (c == 0 && tx == NULL)
  309. mcspi_write_cs_reg(spi,
  310. OMAP2_MCSPI_CHCONF0, l);
  311. *rx++ = __raw_readl(rx_reg);
  312. #ifdef VERBOSE
  313. dev_dbg(&spi->dev, "read-%d %02x\n",
  314. word_len, *(rx - 1));
  315. #endif
  316. }
  317. c -= 1;
  318. } while (c);
  319. } else if (word_len <= 16) {
  320. u16 *rx;
  321. const u16 *tx;
  322. rx = xfer->rx_buf;
  323. tx = xfer->tx_buf;
  324. do {
  325. if (tx != NULL) {
  326. if (mcspi_wait_for_reg_bit(chstat_reg,
  327. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  328. dev_err(&spi->dev, "TXS timed out\n");
  329. goto out;
  330. }
  331. #ifdef VERBOSE
  332. dev_dbg(&spi->dev, "write-%d %04x\n",
  333. word_len, *tx);
  334. #endif
  335. __raw_writel(*tx++, tx_reg);
  336. }
  337. if (rx != NULL) {
  338. if (mcspi_wait_for_reg_bit(chstat_reg,
  339. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  340. dev_err(&spi->dev, "RXS timed out\n");
  341. goto out;
  342. }
  343. /* prevent last RX_ONLY read from triggering
  344. * more word i/o: switch to rx+tx
  345. */
  346. if (c == 0 && tx == NULL)
  347. mcspi_write_cs_reg(spi,
  348. OMAP2_MCSPI_CHCONF0, l);
  349. *rx++ = __raw_readl(rx_reg);
  350. #ifdef VERBOSE
  351. dev_dbg(&spi->dev, "read-%d %04x\n",
  352. word_len, *(rx - 1));
  353. #endif
  354. }
  355. c -= 2;
  356. } while (c);
  357. } else if (word_len <= 32) {
  358. u32 *rx;
  359. const u32 *tx;
  360. rx = xfer->rx_buf;
  361. tx = xfer->tx_buf;
  362. do {
  363. if (tx != NULL) {
  364. if (mcspi_wait_for_reg_bit(chstat_reg,
  365. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  366. dev_err(&spi->dev, "TXS timed out\n");
  367. goto out;
  368. }
  369. #ifdef VERBOSE
  370. dev_dbg(&spi->dev, "write-%d %04x\n",
  371. word_len, *tx);
  372. #endif
  373. __raw_writel(*tx++, tx_reg);
  374. }
  375. if (rx != NULL) {
  376. if (mcspi_wait_for_reg_bit(chstat_reg,
  377. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  378. dev_err(&spi->dev, "RXS timed out\n");
  379. goto out;
  380. }
  381. /* prevent last RX_ONLY read from triggering
  382. * more word i/o: switch to rx+tx
  383. */
  384. if (c == 0 && tx == NULL)
  385. mcspi_write_cs_reg(spi,
  386. OMAP2_MCSPI_CHCONF0, l);
  387. *rx++ = __raw_readl(rx_reg);
  388. #ifdef VERBOSE
  389. dev_dbg(&spi->dev, "read-%d %04x\n",
  390. word_len, *(rx - 1));
  391. #endif
  392. }
  393. c -= 4;
  394. } while (c);
  395. }
  396. /* for TX_ONLY mode, be sure all words have shifted out */
  397. if (xfer->rx_buf == NULL) {
  398. if (mcspi_wait_for_reg_bit(chstat_reg,
  399. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  400. dev_err(&spi->dev, "TXS timed out\n");
  401. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  402. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  403. dev_err(&spi->dev, "EOT timed out\n");
  404. }
  405. out:
  406. return count - c;
  407. }
  408. /* called only when no transfer is active to this device */
  409. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  410. struct spi_transfer *t)
  411. {
  412. struct omap2_mcspi_cs *cs = spi->controller_state;
  413. struct omap2_mcspi *mcspi;
  414. u32 l = 0, div = 0;
  415. u8 word_len = spi->bits_per_word;
  416. mcspi = spi_master_get_devdata(spi->master);
  417. if (t != NULL && t->bits_per_word)
  418. word_len = t->bits_per_word;
  419. cs->word_len = word_len;
  420. if (spi->max_speed_hz) {
  421. while (div <= 15 && (OMAP2_MCSPI_MAX_FREQ / (1 << div))
  422. > spi->max_speed_hz)
  423. div++;
  424. } else
  425. div = 15;
  426. l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  427. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  428. * REVISIT: this controller could support SPI_3WIRE mode.
  429. */
  430. l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
  431. l |= OMAP2_MCSPI_CHCONF_DPE0;
  432. /* wordlength */
  433. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  434. l |= (word_len - 1) << 7;
  435. /* set chipselect polarity; manage with FORCE */
  436. if (!(spi->mode & SPI_CS_HIGH))
  437. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  438. else
  439. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  440. /* set clock divisor */
  441. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  442. l |= div << 2;
  443. /* set SPI mode 0..3 */
  444. if (spi->mode & SPI_CPOL)
  445. l |= OMAP2_MCSPI_CHCONF_POL;
  446. else
  447. l &= ~OMAP2_MCSPI_CHCONF_POL;
  448. if (spi->mode & SPI_CPHA)
  449. l |= OMAP2_MCSPI_CHCONF_PHA;
  450. else
  451. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  452. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, l);
  453. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  454. OMAP2_MCSPI_MAX_FREQ / (1 << div),
  455. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  456. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  457. return 0;
  458. }
  459. static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
  460. {
  461. struct spi_device *spi = data;
  462. struct omap2_mcspi *mcspi;
  463. struct omap2_mcspi_dma *mcspi_dma;
  464. mcspi = spi_master_get_devdata(spi->master);
  465. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  466. complete(&mcspi_dma->dma_rx_completion);
  467. /* We must disable the DMA RX request */
  468. omap2_mcspi_set_dma_req(spi, 1, 0);
  469. }
  470. static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
  471. {
  472. struct spi_device *spi = data;
  473. struct omap2_mcspi *mcspi;
  474. struct omap2_mcspi_dma *mcspi_dma;
  475. mcspi = spi_master_get_devdata(spi->master);
  476. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  477. complete(&mcspi_dma->dma_tx_completion);
  478. /* We must disable the DMA TX request */
  479. omap2_mcspi_set_dma_req(spi, 0, 0);
  480. }
  481. static int omap2_mcspi_request_dma(struct spi_device *spi)
  482. {
  483. struct spi_master *master = spi->master;
  484. struct omap2_mcspi *mcspi;
  485. struct omap2_mcspi_dma *mcspi_dma;
  486. mcspi = spi_master_get_devdata(master);
  487. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  488. if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
  489. omap2_mcspi_dma_rx_callback, spi,
  490. &mcspi_dma->dma_rx_channel)) {
  491. dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
  492. return -EAGAIN;
  493. }
  494. if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
  495. omap2_mcspi_dma_tx_callback, spi,
  496. &mcspi_dma->dma_tx_channel)) {
  497. omap_free_dma(mcspi_dma->dma_rx_channel);
  498. mcspi_dma->dma_rx_channel = -1;
  499. dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
  500. return -EAGAIN;
  501. }
  502. init_completion(&mcspi_dma->dma_rx_completion);
  503. init_completion(&mcspi_dma->dma_tx_completion);
  504. return 0;
  505. }
  506. /* the spi->mode bits understood by this driver: */
  507. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
  508. static int omap2_mcspi_setup(struct spi_device *spi)
  509. {
  510. int ret;
  511. struct omap2_mcspi *mcspi;
  512. struct omap2_mcspi_dma *mcspi_dma;
  513. struct omap2_mcspi_cs *cs = spi->controller_state;
  514. if (spi->mode & ~MODEBITS) {
  515. dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
  516. spi->mode & ~MODEBITS);
  517. return -EINVAL;
  518. }
  519. if (spi->bits_per_word == 0)
  520. spi->bits_per_word = 8;
  521. else if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
  522. dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
  523. spi->bits_per_word);
  524. return -EINVAL;
  525. }
  526. mcspi = spi_master_get_devdata(spi->master);
  527. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  528. if (!cs) {
  529. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  530. if (!cs)
  531. return -ENOMEM;
  532. cs->base = mcspi->base + spi->chip_select * 0x14;
  533. spi->controller_state = cs;
  534. }
  535. if (mcspi_dma->dma_rx_channel == -1
  536. || mcspi_dma->dma_tx_channel == -1) {
  537. ret = omap2_mcspi_request_dma(spi);
  538. if (ret < 0)
  539. return ret;
  540. }
  541. clk_enable(mcspi->ick);
  542. clk_enable(mcspi->fck);
  543. ret = omap2_mcspi_setup_transfer(spi, NULL);
  544. clk_disable(mcspi->fck);
  545. clk_disable(mcspi->ick);
  546. return ret;
  547. }
  548. static void omap2_mcspi_cleanup(struct spi_device *spi)
  549. {
  550. struct omap2_mcspi *mcspi;
  551. struct omap2_mcspi_dma *mcspi_dma;
  552. mcspi = spi_master_get_devdata(spi->master);
  553. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  554. kfree(spi->controller_state);
  555. if (mcspi_dma->dma_rx_channel != -1) {
  556. omap_free_dma(mcspi_dma->dma_rx_channel);
  557. mcspi_dma->dma_rx_channel = -1;
  558. }
  559. if (mcspi_dma->dma_tx_channel != -1) {
  560. omap_free_dma(mcspi_dma->dma_tx_channel);
  561. mcspi_dma->dma_tx_channel = -1;
  562. }
  563. }
  564. static void omap2_mcspi_work(struct work_struct *work)
  565. {
  566. struct omap2_mcspi *mcspi;
  567. mcspi = container_of(work, struct omap2_mcspi, work);
  568. spin_lock_irq(&mcspi->lock);
  569. clk_enable(mcspi->ick);
  570. clk_enable(mcspi->fck);
  571. /* We only enable one channel at a time -- the one whose message is
  572. * at the head of the queue -- although this controller would gladly
  573. * arbitrate among multiple channels. This corresponds to "single
  574. * channel" master mode. As a side effect, we need to manage the
  575. * chipselect with the FORCE bit ... CS != channel enable.
  576. */
  577. while (!list_empty(&mcspi->msg_queue)) {
  578. struct spi_message *m;
  579. struct spi_device *spi;
  580. struct spi_transfer *t = NULL;
  581. int cs_active = 0;
  582. struct omap2_mcspi_device_config *conf;
  583. struct omap2_mcspi_cs *cs;
  584. int par_override = 0;
  585. int status = 0;
  586. u32 chconf;
  587. m = container_of(mcspi->msg_queue.next, struct spi_message,
  588. queue);
  589. list_del_init(&m->queue);
  590. spin_unlock_irq(&mcspi->lock);
  591. spi = m->spi;
  592. conf = spi->controller_data;
  593. cs = spi->controller_state;
  594. omap2_mcspi_set_enable(spi, 1);
  595. list_for_each_entry(t, &m->transfers, transfer_list) {
  596. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  597. status = -EINVAL;
  598. break;
  599. }
  600. if (par_override || t->speed_hz || t->bits_per_word) {
  601. par_override = 1;
  602. status = omap2_mcspi_setup_transfer(spi, t);
  603. if (status < 0)
  604. break;
  605. if (!t->speed_hz && !t->bits_per_word)
  606. par_override = 0;
  607. }
  608. if (!cs_active) {
  609. omap2_mcspi_force_cs(spi, 1);
  610. cs_active = 1;
  611. }
  612. chconf = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  613. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  614. if (t->tx_buf == NULL)
  615. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  616. else if (t->rx_buf == NULL)
  617. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  618. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, chconf);
  619. if (t->len) {
  620. unsigned count;
  621. /* RX_ONLY mode needs dummy data in TX reg */
  622. if (t->tx_buf == NULL)
  623. __raw_writel(0, cs->base
  624. + OMAP2_MCSPI_TX0);
  625. if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
  626. count = omap2_mcspi_txrx_dma(spi, t);
  627. else
  628. count = omap2_mcspi_txrx_pio(spi, t);
  629. m->actual_length += count;
  630. if (count != t->len) {
  631. status = -EIO;
  632. break;
  633. }
  634. }
  635. if (t->delay_usecs)
  636. udelay(t->delay_usecs);
  637. /* ignore the "leave it on after last xfer" hint */
  638. if (t->cs_change) {
  639. omap2_mcspi_force_cs(spi, 0);
  640. cs_active = 0;
  641. }
  642. }
  643. /* Restore defaults if they were overriden */
  644. if (par_override) {
  645. par_override = 0;
  646. status = omap2_mcspi_setup_transfer(spi, NULL);
  647. }
  648. if (cs_active)
  649. omap2_mcspi_force_cs(spi, 0);
  650. omap2_mcspi_set_enable(spi, 0);
  651. m->status = status;
  652. m->complete(m->context);
  653. spin_lock_irq(&mcspi->lock);
  654. }
  655. clk_disable(mcspi->fck);
  656. clk_disable(mcspi->ick);
  657. spin_unlock_irq(&mcspi->lock);
  658. }
  659. static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
  660. {
  661. struct omap2_mcspi *mcspi;
  662. unsigned long flags;
  663. struct spi_transfer *t;
  664. m->actual_length = 0;
  665. m->status = 0;
  666. /* reject invalid messages and transfers */
  667. if (list_empty(&m->transfers) || !m->complete)
  668. return -EINVAL;
  669. list_for_each_entry(t, &m->transfers, transfer_list) {
  670. const void *tx_buf = t->tx_buf;
  671. void *rx_buf = t->rx_buf;
  672. unsigned len = t->len;
  673. if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
  674. || (len && !(rx_buf || tx_buf))
  675. || (t->bits_per_word &&
  676. ( t->bits_per_word < 4
  677. || t->bits_per_word > 32))) {
  678. dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  679. t->speed_hz,
  680. len,
  681. tx_buf ? "tx" : "",
  682. rx_buf ? "rx" : "",
  683. t->bits_per_word);
  684. return -EINVAL;
  685. }
  686. if (t->speed_hz && t->speed_hz < OMAP2_MCSPI_MAX_FREQ/(1<<16)) {
  687. dev_dbg(&spi->dev, "%d Hz max exceeds %d\n",
  688. t->speed_hz,
  689. OMAP2_MCSPI_MAX_FREQ/(1<<16));
  690. return -EINVAL;
  691. }
  692. if (m->is_dma_mapped || len < DMA_MIN_BYTES)
  693. continue;
  694. /* Do DMA mapping "early" for better error reporting and
  695. * dcache use. Note that if dma_unmap_single() ever starts
  696. * to do real work on ARM, we'd need to clean up mappings
  697. * for previous transfers on *ALL* exits of this loop...
  698. */
  699. if (tx_buf != NULL) {
  700. t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
  701. len, DMA_TO_DEVICE);
  702. if (dma_mapping_error(t->tx_dma)) {
  703. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  704. 'T', len);
  705. return -EINVAL;
  706. }
  707. }
  708. if (rx_buf != NULL) {
  709. t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
  710. DMA_FROM_DEVICE);
  711. if (dma_mapping_error(t->rx_dma)) {
  712. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  713. 'R', len);
  714. if (tx_buf != NULL)
  715. dma_unmap_single(NULL, t->tx_dma,
  716. len, DMA_TO_DEVICE);
  717. return -EINVAL;
  718. }
  719. }
  720. }
  721. mcspi = spi_master_get_devdata(spi->master);
  722. spin_lock_irqsave(&mcspi->lock, flags);
  723. list_add_tail(&m->queue, &mcspi->msg_queue);
  724. queue_work(omap2_mcspi_wq, &mcspi->work);
  725. spin_unlock_irqrestore(&mcspi->lock, flags);
  726. return 0;
  727. }
  728. static int __init omap2_mcspi_reset(struct omap2_mcspi *mcspi)
  729. {
  730. struct spi_master *master = mcspi->master;
  731. u32 tmp;
  732. clk_enable(mcspi->ick);
  733. clk_enable(mcspi->fck);
  734. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
  735. OMAP2_MCSPI_SYSCONFIG_SOFTRESET);
  736. do {
  737. tmp = mcspi_read_reg(master, OMAP2_MCSPI_SYSSTATUS);
  738. } while (!(tmp & OMAP2_MCSPI_SYSSTATUS_RESETDONE));
  739. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
  740. /* (3 << 8) | (2 << 3) | */
  741. OMAP2_MCSPI_SYSCONFIG_AUTOIDLE);
  742. omap2_mcspi_set_master_mode(master);
  743. clk_disable(mcspi->fck);
  744. clk_disable(mcspi->ick);
  745. return 0;
  746. }
  747. static u8 __initdata spi1_rxdma_id [] = {
  748. OMAP24XX_DMA_SPI1_RX0,
  749. OMAP24XX_DMA_SPI1_RX1,
  750. OMAP24XX_DMA_SPI1_RX2,
  751. OMAP24XX_DMA_SPI1_RX3,
  752. };
  753. static u8 __initdata spi1_txdma_id [] = {
  754. OMAP24XX_DMA_SPI1_TX0,
  755. OMAP24XX_DMA_SPI1_TX1,
  756. OMAP24XX_DMA_SPI1_TX2,
  757. OMAP24XX_DMA_SPI1_TX3,
  758. };
  759. static u8 __initdata spi2_rxdma_id[] = {
  760. OMAP24XX_DMA_SPI2_RX0,
  761. OMAP24XX_DMA_SPI2_RX1,
  762. };
  763. static u8 __initdata spi2_txdma_id[] = {
  764. OMAP24XX_DMA_SPI2_TX0,
  765. OMAP24XX_DMA_SPI2_TX1,
  766. };
  767. static int __init omap2_mcspi_probe(struct platform_device *pdev)
  768. {
  769. struct spi_master *master;
  770. struct omap2_mcspi *mcspi;
  771. struct resource *r;
  772. int status = 0, i;
  773. const u8 *rxdma_id, *txdma_id;
  774. unsigned num_chipselect;
  775. switch (pdev->id) {
  776. case 1:
  777. rxdma_id = spi1_rxdma_id;
  778. txdma_id = spi1_txdma_id;
  779. num_chipselect = 4;
  780. break;
  781. case 2:
  782. rxdma_id = spi2_rxdma_id;
  783. txdma_id = spi2_txdma_id;
  784. num_chipselect = 2;
  785. break;
  786. /* REVISIT omap2430 has a third McSPI ... */
  787. default:
  788. return -EINVAL;
  789. }
  790. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  791. if (master == NULL) {
  792. dev_dbg(&pdev->dev, "master allocation failed\n");
  793. return -ENOMEM;
  794. }
  795. if (pdev->id != -1)
  796. master->bus_num = pdev->id;
  797. master->setup = omap2_mcspi_setup;
  798. master->transfer = omap2_mcspi_transfer;
  799. master->cleanup = omap2_mcspi_cleanup;
  800. master->num_chipselect = num_chipselect;
  801. dev_set_drvdata(&pdev->dev, master);
  802. mcspi = spi_master_get_devdata(master);
  803. mcspi->master = master;
  804. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  805. if (r == NULL) {
  806. status = -ENODEV;
  807. goto err1;
  808. }
  809. if (!request_mem_region(r->start, (r->end - r->start) + 1,
  810. pdev->dev.bus_id)) {
  811. status = -EBUSY;
  812. goto err1;
  813. }
  814. mcspi->base = (void __iomem *) io_p2v(r->start);
  815. INIT_WORK(&mcspi->work, omap2_mcspi_work);
  816. spin_lock_init(&mcspi->lock);
  817. INIT_LIST_HEAD(&mcspi->msg_queue);
  818. mcspi->ick = clk_get(&pdev->dev, "mcspi_ick");
  819. if (IS_ERR(mcspi->ick)) {
  820. dev_dbg(&pdev->dev, "can't get mcspi_ick\n");
  821. status = PTR_ERR(mcspi->ick);
  822. goto err1a;
  823. }
  824. mcspi->fck = clk_get(&pdev->dev, "mcspi_fck");
  825. if (IS_ERR(mcspi->fck)) {
  826. dev_dbg(&pdev->dev, "can't get mcspi_fck\n");
  827. status = PTR_ERR(mcspi->fck);
  828. goto err2;
  829. }
  830. mcspi->dma_channels = kcalloc(master->num_chipselect,
  831. sizeof(struct omap2_mcspi_dma),
  832. GFP_KERNEL);
  833. if (mcspi->dma_channels == NULL)
  834. goto err3;
  835. for (i = 0; i < num_chipselect; i++) {
  836. mcspi->dma_channels[i].dma_rx_channel = -1;
  837. mcspi->dma_channels[i].dma_rx_sync_dev = rxdma_id[i];
  838. mcspi->dma_channels[i].dma_tx_channel = -1;
  839. mcspi->dma_channels[i].dma_tx_sync_dev = txdma_id[i];
  840. }
  841. if (omap2_mcspi_reset(mcspi) < 0)
  842. goto err4;
  843. status = spi_register_master(master);
  844. if (status < 0)
  845. goto err4;
  846. return status;
  847. err4:
  848. kfree(mcspi->dma_channels);
  849. err3:
  850. clk_put(mcspi->fck);
  851. err2:
  852. clk_put(mcspi->ick);
  853. err1a:
  854. release_mem_region(r->start, (r->end - r->start) + 1);
  855. err1:
  856. spi_master_put(master);
  857. return status;
  858. }
  859. static int __exit omap2_mcspi_remove(struct platform_device *pdev)
  860. {
  861. struct spi_master *master;
  862. struct omap2_mcspi *mcspi;
  863. struct omap2_mcspi_dma *dma_channels;
  864. struct resource *r;
  865. master = dev_get_drvdata(&pdev->dev);
  866. mcspi = spi_master_get_devdata(master);
  867. dma_channels = mcspi->dma_channels;
  868. clk_put(mcspi->fck);
  869. clk_put(mcspi->ick);
  870. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  871. release_mem_region(r->start, (r->end - r->start) + 1);
  872. spi_unregister_master(master);
  873. kfree(dma_channels);
  874. return 0;
  875. }
  876. static struct platform_driver omap2_mcspi_driver = {
  877. .driver = {
  878. .name = "omap2_mcspi",
  879. .owner = THIS_MODULE,
  880. },
  881. .remove = __exit_p(omap2_mcspi_remove),
  882. };
  883. static int __init omap2_mcspi_init(void)
  884. {
  885. omap2_mcspi_wq = create_singlethread_workqueue(
  886. omap2_mcspi_driver.driver.name);
  887. if (omap2_mcspi_wq == NULL)
  888. return -1;
  889. return platform_driver_probe(&omap2_mcspi_driver, omap2_mcspi_probe);
  890. }
  891. subsys_initcall(omap2_mcspi_init);
  892. static void __exit omap2_mcspi_exit(void)
  893. {
  894. platform_driver_unregister(&omap2_mcspi_driver);
  895. destroy_workqueue(omap2_mcspi_wq);
  896. }
  897. module_exit(omap2_mcspi_exit);
  898. MODULE_LICENSE("GPL");