omap2_mcspi.c 32 KB

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