omap2_mcspi.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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. mcspi = spi_master_get_devdata(spi->master);
  495. spi_cntrl = mcspi->master;
  496. if (t != NULL && t->bits_per_word)
  497. word_len = t->bits_per_word;
  498. cs->word_len = word_len;
  499. if (spi->max_speed_hz) {
  500. while (div <= 15 && (OMAP2_MCSPI_MAX_FREQ / (1 << div))
  501. > spi->max_speed_hz)
  502. div++;
  503. } else
  504. div = 15;
  505. l = mcspi_cached_chconf0(spi);
  506. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  507. * REVISIT: this controller could support SPI_3WIRE mode.
  508. */
  509. l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
  510. l |= OMAP2_MCSPI_CHCONF_DPE0;
  511. /* wordlength */
  512. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  513. l |= (word_len - 1) << 7;
  514. /* set chipselect polarity; manage with FORCE */
  515. if (!(spi->mode & SPI_CS_HIGH))
  516. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  517. else
  518. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  519. /* set clock divisor */
  520. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  521. l |= div << 2;
  522. /* set SPI mode 0..3 */
  523. if (spi->mode & SPI_CPOL)
  524. l |= OMAP2_MCSPI_CHCONF_POL;
  525. else
  526. l &= ~OMAP2_MCSPI_CHCONF_POL;
  527. if (spi->mode & SPI_CPHA)
  528. l |= OMAP2_MCSPI_CHCONF_PHA;
  529. else
  530. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  531. mcspi_write_chconf0(spi, l);
  532. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  533. OMAP2_MCSPI_MAX_FREQ / (1 << div),
  534. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  535. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  536. return 0;
  537. }
  538. static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
  539. {
  540. struct spi_device *spi = data;
  541. struct omap2_mcspi *mcspi;
  542. struct omap2_mcspi_dma *mcspi_dma;
  543. mcspi = spi_master_get_devdata(spi->master);
  544. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  545. complete(&mcspi_dma->dma_rx_completion);
  546. /* We must disable the DMA RX request */
  547. omap2_mcspi_set_dma_req(spi, 1, 0);
  548. }
  549. static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
  550. {
  551. struct spi_device *spi = data;
  552. struct omap2_mcspi *mcspi;
  553. struct omap2_mcspi_dma *mcspi_dma;
  554. mcspi = spi_master_get_devdata(spi->master);
  555. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  556. complete(&mcspi_dma->dma_tx_completion);
  557. /* We must disable the DMA TX request */
  558. omap2_mcspi_set_dma_req(spi, 0, 0);
  559. }
  560. static int omap2_mcspi_request_dma(struct spi_device *spi)
  561. {
  562. struct spi_master *master = spi->master;
  563. struct omap2_mcspi *mcspi;
  564. struct omap2_mcspi_dma *mcspi_dma;
  565. mcspi = spi_master_get_devdata(master);
  566. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  567. if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
  568. omap2_mcspi_dma_rx_callback, spi,
  569. &mcspi_dma->dma_rx_channel)) {
  570. dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
  571. return -EAGAIN;
  572. }
  573. if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
  574. omap2_mcspi_dma_tx_callback, spi,
  575. &mcspi_dma->dma_tx_channel)) {
  576. omap_free_dma(mcspi_dma->dma_rx_channel);
  577. mcspi_dma->dma_rx_channel = -1;
  578. dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
  579. return -EAGAIN;
  580. }
  581. init_completion(&mcspi_dma->dma_rx_completion);
  582. init_completion(&mcspi_dma->dma_tx_completion);
  583. return 0;
  584. }
  585. static int omap2_mcspi_setup(struct spi_device *spi)
  586. {
  587. int ret;
  588. struct omap2_mcspi *mcspi;
  589. struct omap2_mcspi_dma *mcspi_dma;
  590. struct omap2_mcspi_cs *cs = spi->controller_state;
  591. if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
  592. dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
  593. spi->bits_per_word);
  594. return -EINVAL;
  595. }
  596. mcspi = spi_master_get_devdata(spi->master);
  597. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  598. if (!cs) {
  599. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  600. if (!cs)
  601. return -ENOMEM;
  602. cs->base = mcspi->base + spi->chip_select * 0x14;
  603. cs->phys = mcspi->phys + spi->chip_select * 0x14;
  604. cs->chconf0 = 0;
  605. spi->controller_state = cs;
  606. /* Link this to context save list */
  607. list_add_tail(&cs->node,
  608. &omap2_mcspi_ctx[mcspi->master->bus_num - 1].cs);
  609. }
  610. if (mcspi_dma->dma_rx_channel == -1
  611. || mcspi_dma->dma_tx_channel == -1) {
  612. ret = omap2_mcspi_request_dma(spi);
  613. if (ret < 0)
  614. return ret;
  615. }
  616. if (omap2_mcspi_enable_clocks(mcspi))
  617. return -ENODEV;
  618. ret = omap2_mcspi_setup_transfer(spi, NULL);
  619. omap2_mcspi_disable_clocks(mcspi);
  620. return ret;
  621. }
  622. static void omap2_mcspi_cleanup(struct spi_device *spi)
  623. {
  624. struct omap2_mcspi *mcspi;
  625. struct omap2_mcspi_dma *mcspi_dma;
  626. struct omap2_mcspi_cs *cs;
  627. mcspi = spi_master_get_devdata(spi->master);
  628. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  629. /* Unlink controller state from context save list */
  630. cs = spi->controller_state;
  631. list_del(&cs->node);
  632. kfree(spi->controller_state);
  633. if (mcspi_dma->dma_rx_channel != -1) {
  634. omap_free_dma(mcspi_dma->dma_rx_channel);
  635. mcspi_dma->dma_rx_channel = -1;
  636. }
  637. if (mcspi_dma->dma_tx_channel != -1) {
  638. omap_free_dma(mcspi_dma->dma_tx_channel);
  639. mcspi_dma->dma_tx_channel = -1;
  640. }
  641. }
  642. static void omap2_mcspi_work(struct work_struct *work)
  643. {
  644. struct omap2_mcspi *mcspi;
  645. mcspi = container_of(work, struct omap2_mcspi, work);
  646. spin_lock_irq(&mcspi->lock);
  647. if (omap2_mcspi_enable_clocks(mcspi))
  648. goto out;
  649. /* We only enable one channel at a time -- the one whose message is
  650. * at the head of the queue -- although this controller would gladly
  651. * arbitrate among multiple channels. This corresponds to "single
  652. * channel" master mode. As a side effect, we need to manage the
  653. * chipselect with the FORCE bit ... CS != channel enable.
  654. */
  655. while (!list_empty(&mcspi->msg_queue)) {
  656. struct spi_message *m;
  657. struct spi_device *spi;
  658. struct spi_transfer *t = NULL;
  659. int cs_active = 0;
  660. struct omap2_mcspi_cs *cs;
  661. int par_override = 0;
  662. int status = 0;
  663. u32 chconf;
  664. m = container_of(mcspi->msg_queue.next, struct spi_message,
  665. queue);
  666. list_del_init(&m->queue);
  667. spin_unlock_irq(&mcspi->lock);
  668. spi = m->spi;
  669. cs = spi->controller_state;
  670. omap2_mcspi_set_enable(spi, 1);
  671. list_for_each_entry(t, &m->transfers, transfer_list) {
  672. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  673. status = -EINVAL;
  674. break;
  675. }
  676. if (par_override || t->speed_hz || t->bits_per_word) {
  677. par_override = 1;
  678. status = omap2_mcspi_setup_transfer(spi, t);
  679. if (status < 0)
  680. break;
  681. if (!t->speed_hz && !t->bits_per_word)
  682. par_override = 0;
  683. }
  684. if (!cs_active) {
  685. omap2_mcspi_force_cs(spi, 1);
  686. cs_active = 1;
  687. }
  688. chconf = mcspi_cached_chconf0(spi);
  689. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  690. if (t->tx_buf == NULL)
  691. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  692. else if (t->rx_buf == NULL)
  693. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  694. mcspi_write_chconf0(spi, chconf);
  695. if (t->len) {
  696. unsigned count;
  697. /* RX_ONLY mode needs dummy data in TX reg */
  698. if (t->tx_buf == NULL)
  699. __raw_writel(0, cs->base
  700. + OMAP2_MCSPI_TX0);
  701. if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
  702. count = omap2_mcspi_txrx_dma(spi, t);
  703. else
  704. count = omap2_mcspi_txrx_pio(spi, t);
  705. m->actual_length += count;
  706. if (count != t->len) {
  707. status = -EIO;
  708. break;
  709. }
  710. }
  711. if (t->delay_usecs)
  712. udelay(t->delay_usecs);
  713. /* ignore the "leave it on after last xfer" hint */
  714. if (t->cs_change) {
  715. omap2_mcspi_force_cs(spi, 0);
  716. cs_active = 0;
  717. }
  718. }
  719. /* Restore defaults if they were overriden */
  720. if (par_override) {
  721. par_override = 0;
  722. status = omap2_mcspi_setup_transfer(spi, NULL);
  723. }
  724. if (cs_active)
  725. omap2_mcspi_force_cs(spi, 0);
  726. omap2_mcspi_set_enable(spi, 0);
  727. m->status = status;
  728. m->complete(m->context);
  729. spin_lock_irq(&mcspi->lock);
  730. }
  731. omap2_mcspi_disable_clocks(mcspi);
  732. out:
  733. spin_unlock_irq(&mcspi->lock);
  734. }
  735. static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
  736. {
  737. struct omap2_mcspi *mcspi;
  738. unsigned long flags;
  739. struct spi_transfer *t;
  740. m->actual_length = 0;
  741. m->status = 0;
  742. /* reject invalid messages and transfers */
  743. if (list_empty(&m->transfers) || !m->complete)
  744. return -EINVAL;
  745. list_for_each_entry(t, &m->transfers, transfer_list) {
  746. const void *tx_buf = t->tx_buf;
  747. void *rx_buf = t->rx_buf;
  748. unsigned len = t->len;
  749. if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
  750. || (len && !(rx_buf || tx_buf))
  751. || (t->bits_per_word &&
  752. ( t->bits_per_word < 4
  753. || t->bits_per_word > 32))) {
  754. dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  755. t->speed_hz,
  756. len,
  757. tx_buf ? "tx" : "",
  758. rx_buf ? "rx" : "",
  759. t->bits_per_word);
  760. return -EINVAL;
  761. }
  762. if (t->speed_hz && t->speed_hz < OMAP2_MCSPI_MAX_FREQ/(1<<16)) {
  763. dev_dbg(&spi->dev, "%d Hz max exceeds %d\n",
  764. t->speed_hz,
  765. OMAP2_MCSPI_MAX_FREQ/(1<<16));
  766. return -EINVAL;
  767. }
  768. if (m->is_dma_mapped || len < DMA_MIN_BYTES)
  769. continue;
  770. /* Do DMA mapping "early" for better error reporting and
  771. * dcache use. Note that if dma_unmap_single() ever starts
  772. * to do real work on ARM, we'd need to clean up mappings
  773. * for previous transfers on *ALL* exits of this loop...
  774. */
  775. if (tx_buf != NULL) {
  776. t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
  777. len, DMA_TO_DEVICE);
  778. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  779. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  780. 'T', len);
  781. return -EINVAL;
  782. }
  783. }
  784. if (rx_buf != NULL) {
  785. t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
  786. DMA_FROM_DEVICE);
  787. if (dma_mapping_error(&spi->dev, t->rx_dma)) {
  788. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  789. 'R', len);
  790. if (tx_buf != NULL)
  791. dma_unmap_single(NULL, t->tx_dma,
  792. len, DMA_TO_DEVICE);
  793. return -EINVAL;
  794. }
  795. }
  796. }
  797. mcspi = spi_master_get_devdata(spi->master);
  798. spin_lock_irqsave(&mcspi->lock, flags);
  799. list_add_tail(&m->queue, &mcspi->msg_queue);
  800. queue_work(omap2_mcspi_wq, &mcspi->work);
  801. spin_unlock_irqrestore(&mcspi->lock, flags);
  802. return 0;
  803. }
  804. static int __init omap2_mcspi_reset(struct omap2_mcspi *mcspi)
  805. {
  806. struct spi_master *master = mcspi->master;
  807. u32 tmp;
  808. if (omap2_mcspi_enable_clocks(mcspi))
  809. return -1;
  810. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
  811. OMAP2_MCSPI_SYSCONFIG_SOFTRESET);
  812. do {
  813. tmp = mcspi_read_reg(master, OMAP2_MCSPI_SYSSTATUS);
  814. } while (!(tmp & OMAP2_MCSPI_SYSSTATUS_RESETDONE));
  815. tmp = OMAP2_MCSPI_SYSCONFIG_AUTOIDLE |
  816. OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP |
  817. OMAP2_MCSPI_SYSCONFIG_SMARTIDLE;
  818. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG, tmp);
  819. omap2_mcspi_ctx[master->bus_num - 1].sysconfig = tmp;
  820. tmp = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  821. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, tmp);
  822. omap2_mcspi_ctx[master->bus_num - 1].wakeupenable = tmp;
  823. omap2_mcspi_set_master_mode(master);
  824. omap2_mcspi_disable_clocks(mcspi);
  825. return 0;
  826. }
  827. static u8 __initdata spi1_rxdma_id [] = {
  828. OMAP24XX_DMA_SPI1_RX0,
  829. OMAP24XX_DMA_SPI1_RX1,
  830. OMAP24XX_DMA_SPI1_RX2,
  831. OMAP24XX_DMA_SPI1_RX3,
  832. };
  833. static u8 __initdata spi1_txdma_id [] = {
  834. OMAP24XX_DMA_SPI1_TX0,
  835. OMAP24XX_DMA_SPI1_TX1,
  836. OMAP24XX_DMA_SPI1_TX2,
  837. OMAP24XX_DMA_SPI1_TX3,
  838. };
  839. static u8 __initdata spi2_rxdma_id[] = {
  840. OMAP24XX_DMA_SPI2_RX0,
  841. OMAP24XX_DMA_SPI2_RX1,
  842. };
  843. static u8 __initdata spi2_txdma_id[] = {
  844. OMAP24XX_DMA_SPI2_TX0,
  845. OMAP24XX_DMA_SPI2_TX1,
  846. };
  847. #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \
  848. || defined(CONFIG_ARCH_OMAP4)
  849. static u8 __initdata spi3_rxdma_id[] = {
  850. OMAP24XX_DMA_SPI3_RX0,
  851. OMAP24XX_DMA_SPI3_RX1,
  852. };
  853. static u8 __initdata spi3_txdma_id[] = {
  854. OMAP24XX_DMA_SPI3_TX0,
  855. OMAP24XX_DMA_SPI3_TX1,
  856. };
  857. #endif
  858. #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
  859. static u8 __initdata spi4_rxdma_id[] = {
  860. OMAP34XX_DMA_SPI4_RX0,
  861. };
  862. static u8 __initdata spi4_txdma_id[] = {
  863. OMAP34XX_DMA_SPI4_TX0,
  864. };
  865. #endif
  866. static int __init omap2_mcspi_probe(struct platform_device *pdev)
  867. {
  868. struct spi_master *master;
  869. struct omap2_mcspi *mcspi;
  870. struct resource *r;
  871. int status = 0, i;
  872. const u8 *rxdma_id, *txdma_id;
  873. unsigned num_chipselect;
  874. switch (pdev->id) {
  875. case 1:
  876. rxdma_id = spi1_rxdma_id;
  877. txdma_id = spi1_txdma_id;
  878. num_chipselect = 4;
  879. break;
  880. case 2:
  881. rxdma_id = spi2_rxdma_id;
  882. txdma_id = spi2_txdma_id;
  883. num_chipselect = 2;
  884. break;
  885. #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \
  886. || defined(CONFIG_ARCH_OMAP4)
  887. case 3:
  888. rxdma_id = spi3_rxdma_id;
  889. txdma_id = spi3_txdma_id;
  890. num_chipselect = 2;
  891. break;
  892. #endif
  893. #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
  894. case 4:
  895. rxdma_id = spi4_rxdma_id;
  896. txdma_id = spi4_txdma_id;
  897. num_chipselect = 1;
  898. break;
  899. #endif
  900. default:
  901. return -EINVAL;
  902. }
  903. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  904. if (master == NULL) {
  905. dev_dbg(&pdev->dev, "master allocation failed\n");
  906. return -ENOMEM;
  907. }
  908. /* the spi->mode bits understood by this driver: */
  909. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  910. if (pdev->id != -1)
  911. master->bus_num = pdev->id;
  912. master->setup = omap2_mcspi_setup;
  913. master->transfer = omap2_mcspi_transfer;
  914. master->cleanup = omap2_mcspi_cleanup;
  915. master->num_chipselect = num_chipselect;
  916. dev_set_drvdata(&pdev->dev, master);
  917. mcspi = spi_master_get_devdata(master);
  918. mcspi->master = master;
  919. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  920. if (r == NULL) {
  921. status = -ENODEV;
  922. goto err1;
  923. }
  924. if (!request_mem_region(r->start, (r->end - r->start) + 1,
  925. dev_name(&pdev->dev))) {
  926. status = -EBUSY;
  927. goto err1;
  928. }
  929. mcspi->phys = r->start;
  930. mcspi->base = ioremap(r->start, r->end - r->start + 1);
  931. if (!mcspi->base) {
  932. dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
  933. status = -ENOMEM;
  934. goto err1aa;
  935. }
  936. INIT_WORK(&mcspi->work, omap2_mcspi_work);
  937. spin_lock_init(&mcspi->lock);
  938. INIT_LIST_HEAD(&mcspi->msg_queue);
  939. INIT_LIST_HEAD(&omap2_mcspi_ctx[master->bus_num - 1].cs);
  940. mcspi->ick = clk_get(&pdev->dev, "ick");
  941. if (IS_ERR(mcspi->ick)) {
  942. dev_dbg(&pdev->dev, "can't get mcspi_ick\n");
  943. status = PTR_ERR(mcspi->ick);
  944. goto err1a;
  945. }
  946. mcspi->fck = clk_get(&pdev->dev, "fck");
  947. if (IS_ERR(mcspi->fck)) {
  948. dev_dbg(&pdev->dev, "can't get mcspi_fck\n");
  949. status = PTR_ERR(mcspi->fck);
  950. goto err2;
  951. }
  952. mcspi->dma_channels = kcalloc(master->num_chipselect,
  953. sizeof(struct omap2_mcspi_dma),
  954. GFP_KERNEL);
  955. if (mcspi->dma_channels == NULL)
  956. goto err3;
  957. for (i = 0; i < num_chipselect; i++) {
  958. mcspi->dma_channels[i].dma_rx_channel = -1;
  959. mcspi->dma_channels[i].dma_rx_sync_dev = rxdma_id[i];
  960. mcspi->dma_channels[i].dma_tx_channel = -1;
  961. mcspi->dma_channels[i].dma_tx_sync_dev = txdma_id[i];
  962. }
  963. if (omap2_mcspi_reset(mcspi) < 0)
  964. goto err4;
  965. status = spi_register_master(master);
  966. if (status < 0)
  967. goto err4;
  968. return status;
  969. err4:
  970. kfree(mcspi->dma_channels);
  971. err3:
  972. clk_put(mcspi->fck);
  973. err2:
  974. clk_put(mcspi->ick);
  975. err1a:
  976. iounmap(mcspi->base);
  977. err1aa:
  978. release_mem_region(r->start, (r->end - r->start) + 1);
  979. err1:
  980. spi_master_put(master);
  981. return status;
  982. }
  983. static int __exit omap2_mcspi_remove(struct platform_device *pdev)
  984. {
  985. struct spi_master *master;
  986. struct omap2_mcspi *mcspi;
  987. struct omap2_mcspi_dma *dma_channels;
  988. struct resource *r;
  989. void __iomem *base;
  990. master = dev_get_drvdata(&pdev->dev);
  991. mcspi = spi_master_get_devdata(master);
  992. dma_channels = mcspi->dma_channels;
  993. clk_put(mcspi->fck);
  994. clk_put(mcspi->ick);
  995. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  996. release_mem_region(r->start, (r->end - r->start) + 1);
  997. base = mcspi->base;
  998. spi_unregister_master(master);
  999. iounmap(base);
  1000. kfree(dma_channels);
  1001. return 0;
  1002. }
  1003. /* work with hotplug and coldplug */
  1004. MODULE_ALIAS("platform:omap2_mcspi");
  1005. static struct platform_driver omap2_mcspi_driver = {
  1006. .driver = {
  1007. .name = "omap2_mcspi",
  1008. .owner = THIS_MODULE,
  1009. },
  1010. .remove = __exit_p(omap2_mcspi_remove),
  1011. };
  1012. static int __init omap2_mcspi_init(void)
  1013. {
  1014. omap2_mcspi_wq = create_singlethread_workqueue(
  1015. omap2_mcspi_driver.driver.name);
  1016. if (omap2_mcspi_wq == NULL)
  1017. return -1;
  1018. return platform_driver_probe(&omap2_mcspi_driver, omap2_mcspi_probe);
  1019. }
  1020. subsys_initcall(omap2_mcspi_init);
  1021. static void __exit omap2_mcspi_exit(void)
  1022. {
  1023. platform_driver_unregister(&omap2_mcspi_driver);
  1024. destroy_workqueue(omap2_mcspi_wq);
  1025. }
  1026. module_exit(omap2_mcspi_exit);
  1027. MODULE_LICENSE("GPL");