omap2_mcspi.c 30 KB

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