omap2_mcspi.c 27 KB

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