omap2_mcspi.c 28 KB

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