spi-fsl-spi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. /*
  2. * Freescale SPI controller driver.
  3. *
  4. * Maintainer: Kumar Gala
  5. *
  6. * Copyright (C) 2006 Polycom, Inc.
  7. * Copyright 2010 Freescale Semiconductor, Inc.
  8. *
  9. * CPM SPI and QE buffer descriptors mode support:
  10. * Copyright (c) 2009 MontaVista Software, Inc.
  11. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  12. *
  13. * GRLIB support:
  14. * Copyright (c) 2012 Aeroflex Gaisler AB.
  15. * Author: Andreas Larsson <andreas@gaisler.com>
  16. *
  17. * This program is free software; you can redistribute it and/or modify it
  18. * under the terms of the GNU General Public License as published by the
  19. * Free Software Foundation; either version 2 of the License, or (at your
  20. * option) any later version.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/types.h>
  24. #include <linux/kernel.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/delay.h>
  27. #include <linux/irq.h>
  28. #include <linux/spi/spi.h>
  29. #include <linux/spi/spi_bitbang.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/fsl_devices.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/mm.h>
  34. #include <linux/mutex.h>
  35. #include <linux/of.h>
  36. #include <linux/of_platform.h>
  37. #include <linux/of_address.h>
  38. #include <linux/of_irq.h>
  39. #include <linux/gpio.h>
  40. #include <linux/of_gpio.h>
  41. #include "spi-fsl-lib.h"
  42. #include "spi-fsl-cpm.h"
  43. #include "spi-fsl-spi.h"
  44. #define TYPE_FSL 0
  45. #define TYPE_GRLIB 1
  46. struct fsl_spi_match_data {
  47. int type;
  48. };
  49. static struct fsl_spi_match_data of_fsl_spi_fsl_config = {
  50. .type = TYPE_FSL,
  51. };
  52. static struct fsl_spi_match_data of_fsl_spi_grlib_config = {
  53. .type = TYPE_GRLIB,
  54. };
  55. static struct of_device_id of_fsl_spi_match[] = {
  56. {
  57. .compatible = "fsl,spi",
  58. .data = &of_fsl_spi_fsl_config,
  59. },
  60. {
  61. .compatible = "aeroflexgaisler,spictrl",
  62. .data = &of_fsl_spi_grlib_config,
  63. },
  64. {}
  65. };
  66. MODULE_DEVICE_TABLE(of, of_fsl_spi_match);
  67. static int fsl_spi_get_type(struct device *dev)
  68. {
  69. const struct of_device_id *match;
  70. if (dev->of_node) {
  71. match = of_match_node(of_fsl_spi_match, dev->of_node);
  72. if (match && match->data)
  73. return ((struct fsl_spi_match_data *)match->data)->type;
  74. }
  75. return TYPE_FSL;
  76. }
  77. static void fsl_spi_change_mode(struct spi_device *spi)
  78. {
  79. struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
  80. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  81. struct fsl_spi_reg *reg_base = mspi->reg_base;
  82. __be32 __iomem *mode = &reg_base->mode;
  83. unsigned long flags;
  84. if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
  85. return;
  86. /* Turn off IRQs locally to minimize time that SPI is disabled. */
  87. local_irq_save(flags);
  88. /* Turn off SPI unit prior changing mode */
  89. mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
  90. /* When in CPM mode, we need to reinit tx and rx. */
  91. if (mspi->flags & SPI_CPM_MODE) {
  92. fsl_spi_cpm_reinit_txrx(mspi);
  93. }
  94. mpc8xxx_spi_write_reg(mode, cs->hw_mode);
  95. local_irq_restore(flags);
  96. }
  97. static void fsl_spi_chipselect(struct spi_device *spi, int value)
  98. {
  99. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  100. struct fsl_spi_platform_data *pdata;
  101. bool pol = spi->mode & SPI_CS_HIGH;
  102. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  103. pdata = spi->dev.parent->parent->platform_data;
  104. if (value == BITBANG_CS_INACTIVE) {
  105. if (pdata->cs_control)
  106. pdata->cs_control(spi, !pol);
  107. }
  108. if (value == BITBANG_CS_ACTIVE) {
  109. mpc8xxx_spi->rx_shift = cs->rx_shift;
  110. mpc8xxx_spi->tx_shift = cs->tx_shift;
  111. mpc8xxx_spi->get_rx = cs->get_rx;
  112. mpc8xxx_spi->get_tx = cs->get_tx;
  113. fsl_spi_change_mode(spi);
  114. if (pdata->cs_control)
  115. pdata->cs_control(spi, pol);
  116. }
  117. }
  118. static void fsl_spi_qe_cpu_set_shifts(u32 *rx_shift, u32 *tx_shift,
  119. int bits_per_word, int msb_first)
  120. {
  121. *rx_shift = 0;
  122. *tx_shift = 0;
  123. if (msb_first) {
  124. if (bits_per_word <= 8) {
  125. *rx_shift = 16;
  126. *tx_shift = 24;
  127. } else if (bits_per_word <= 16) {
  128. *rx_shift = 16;
  129. *tx_shift = 16;
  130. }
  131. } else {
  132. if (bits_per_word <= 8)
  133. *rx_shift = 8;
  134. }
  135. }
  136. static void fsl_spi_grlib_set_shifts(u32 *rx_shift, u32 *tx_shift,
  137. int bits_per_word, int msb_first)
  138. {
  139. *rx_shift = 0;
  140. *tx_shift = 0;
  141. if (bits_per_word <= 16) {
  142. if (msb_first) {
  143. *rx_shift = 16; /* LSB in bit 16 */
  144. *tx_shift = 32 - bits_per_word; /* MSB in bit 31 */
  145. } else {
  146. *rx_shift = 16 - bits_per_word; /* MSB in bit 15 */
  147. }
  148. }
  149. }
  150. static int mspi_apply_cpu_mode_quirks(struct spi_mpc8xxx_cs *cs,
  151. struct spi_device *spi,
  152. struct mpc8xxx_spi *mpc8xxx_spi,
  153. int bits_per_word)
  154. {
  155. cs->rx_shift = 0;
  156. cs->tx_shift = 0;
  157. if (bits_per_word <= 8) {
  158. cs->get_rx = mpc8xxx_spi_rx_buf_u8;
  159. cs->get_tx = mpc8xxx_spi_tx_buf_u8;
  160. } else if (bits_per_word <= 16) {
  161. cs->get_rx = mpc8xxx_spi_rx_buf_u16;
  162. cs->get_tx = mpc8xxx_spi_tx_buf_u16;
  163. } else if (bits_per_word <= 32) {
  164. cs->get_rx = mpc8xxx_spi_rx_buf_u32;
  165. cs->get_tx = mpc8xxx_spi_tx_buf_u32;
  166. } else
  167. return -EINVAL;
  168. if (mpc8xxx_spi->set_shifts)
  169. mpc8xxx_spi->set_shifts(&cs->rx_shift, &cs->tx_shift,
  170. bits_per_word,
  171. !(spi->mode & SPI_LSB_FIRST));
  172. mpc8xxx_spi->rx_shift = cs->rx_shift;
  173. mpc8xxx_spi->tx_shift = cs->tx_shift;
  174. mpc8xxx_spi->get_rx = cs->get_rx;
  175. mpc8xxx_spi->get_tx = cs->get_tx;
  176. return bits_per_word;
  177. }
  178. static int mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs *cs,
  179. struct spi_device *spi,
  180. int bits_per_word)
  181. {
  182. /* QE uses Little Endian for words > 8
  183. * so transform all words > 8 into 8 bits
  184. * Unfortnatly that doesn't work for LSB so
  185. * reject these for now */
  186. /* Note: 32 bits word, LSB works iff
  187. * tfcr/rfcr is set to CPMFCR_GBL */
  188. if (spi->mode & SPI_LSB_FIRST &&
  189. bits_per_word > 8)
  190. return -EINVAL;
  191. if (bits_per_word > 8)
  192. return 8; /* pretend its 8 bits */
  193. return bits_per_word;
  194. }
  195. static int fsl_spi_setup_transfer(struct spi_device *spi,
  196. struct spi_transfer *t)
  197. {
  198. struct mpc8xxx_spi *mpc8xxx_spi;
  199. int bits_per_word = 0;
  200. u8 pm;
  201. u32 hz = 0;
  202. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  203. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  204. if (t) {
  205. bits_per_word = t->bits_per_word;
  206. hz = t->speed_hz;
  207. }
  208. /* spi_transfer level calls that work per-word */
  209. if (!bits_per_word)
  210. bits_per_word = spi->bits_per_word;
  211. /* Make sure its a bit width we support [4..16, 32] */
  212. if ((bits_per_word < 4)
  213. || ((bits_per_word > 16) && (bits_per_word != 32))
  214. || (bits_per_word > mpc8xxx_spi->max_bits_per_word))
  215. return -EINVAL;
  216. if (!hz)
  217. hz = spi->max_speed_hz;
  218. if (!(mpc8xxx_spi->flags & SPI_CPM_MODE))
  219. bits_per_word = mspi_apply_cpu_mode_quirks(cs, spi,
  220. mpc8xxx_spi,
  221. bits_per_word);
  222. else if (mpc8xxx_spi->flags & SPI_QE)
  223. bits_per_word = mspi_apply_qe_mode_quirks(cs, spi,
  224. bits_per_word);
  225. if (bits_per_word < 0)
  226. return bits_per_word;
  227. if (bits_per_word == 32)
  228. bits_per_word = 0;
  229. else
  230. bits_per_word = bits_per_word - 1;
  231. /* mask out bits we are going to set */
  232. cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
  233. | SPMODE_PM(0xF));
  234. cs->hw_mode |= SPMODE_LEN(bits_per_word);
  235. if ((mpc8xxx_spi->spibrg / hz) > 64) {
  236. cs->hw_mode |= SPMODE_DIV16;
  237. pm = (mpc8xxx_spi->spibrg - 1) / (hz * 64) + 1;
  238. WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
  239. "Will use %d Hz instead.\n", dev_name(&spi->dev),
  240. hz, mpc8xxx_spi->spibrg / 1024);
  241. if (pm > 16)
  242. pm = 16;
  243. } else {
  244. pm = (mpc8xxx_spi->spibrg - 1) / (hz * 4) + 1;
  245. }
  246. if (pm)
  247. pm--;
  248. cs->hw_mode |= SPMODE_PM(pm);
  249. fsl_spi_change_mode(spi);
  250. return 0;
  251. }
  252. static int fsl_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
  253. struct spi_transfer *t, unsigned int len)
  254. {
  255. u32 word;
  256. struct fsl_spi_reg *reg_base = mspi->reg_base;
  257. mspi->count = len;
  258. /* enable rx ints */
  259. mpc8xxx_spi_write_reg(&reg_base->mask, SPIM_NE);
  260. /* transmit word */
  261. word = mspi->get_tx(mspi);
  262. mpc8xxx_spi_write_reg(&reg_base->transmit, word);
  263. return 0;
  264. }
  265. static int fsl_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
  266. bool is_dma_mapped)
  267. {
  268. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  269. struct fsl_spi_reg *reg_base;
  270. unsigned int len = t->len;
  271. u8 bits_per_word;
  272. int ret;
  273. reg_base = mpc8xxx_spi->reg_base;
  274. bits_per_word = spi->bits_per_word;
  275. if (t->bits_per_word)
  276. bits_per_word = t->bits_per_word;
  277. if (bits_per_word > 8) {
  278. /* invalid length? */
  279. if (len & 1)
  280. return -EINVAL;
  281. len /= 2;
  282. }
  283. if (bits_per_word > 16) {
  284. /* invalid length? */
  285. if (len & 1)
  286. return -EINVAL;
  287. len /= 2;
  288. }
  289. mpc8xxx_spi->tx = t->tx_buf;
  290. mpc8xxx_spi->rx = t->rx_buf;
  291. INIT_COMPLETION(mpc8xxx_spi->done);
  292. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  293. ret = fsl_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
  294. else
  295. ret = fsl_spi_cpu_bufs(mpc8xxx_spi, t, len);
  296. if (ret)
  297. return ret;
  298. wait_for_completion(&mpc8xxx_spi->done);
  299. /* disable rx ints */
  300. mpc8xxx_spi_write_reg(&reg_base->mask, 0);
  301. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  302. fsl_spi_cpm_bufs_complete(mpc8xxx_spi);
  303. return mpc8xxx_spi->count;
  304. }
  305. static void fsl_spi_do_one_msg(struct spi_message *m)
  306. {
  307. struct spi_device *spi = m->spi;
  308. struct spi_transfer *t;
  309. unsigned int cs_change;
  310. const int nsecs = 50;
  311. int status;
  312. cs_change = 1;
  313. status = 0;
  314. list_for_each_entry(t, &m->transfers, transfer_list) {
  315. if (t->bits_per_word || t->speed_hz) {
  316. /* Don't allow changes if CS is active */
  317. status = -EINVAL;
  318. if (cs_change)
  319. status = fsl_spi_setup_transfer(spi, t);
  320. if (status < 0)
  321. break;
  322. }
  323. if (cs_change) {
  324. fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE);
  325. ndelay(nsecs);
  326. }
  327. cs_change = t->cs_change;
  328. if (t->len)
  329. status = fsl_spi_bufs(spi, t, m->is_dma_mapped);
  330. if (status) {
  331. status = -EMSGSIZE;
  332. break;
  333. }
  334. m->actual_length += t->len;
  335. if (t->delay_usecs)
  336. udelay(t->delay_usecs);
  337. if (cs_change) {
  338. ndelay(nsecs);
  339. fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  340. ndelay(nsecs);
  341. }
  342. }
  343. m->status = status;
  344. m->complete(m->context);
  345. if (status || !cs_change) {
  346. ndelay(nsecs);
  347. fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  348. }
  349. fsl_spi_setup_transfer(spi, NULL);
  350. }
  351. static int fsl_spi_setup(struct spi_device *spi)
  352. {
  353. struct mpc8xxx_spi *mpc8xxx_spi;
  354. struct fsl_spi_reg *reg_base;
  355. int retval;
  356. u32 hw_mode;
  357. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  358. if (!spi->max_speed_hz)
  359. return -EINVAL;
  360. if (!cs) {
  361. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  362. if (!cs)
  363. return -ENOMEM;
  364. spi->controller_state = cs;
  365. }
  366. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  367. reg_base = mpc8xxx_spi->reg_base;
  368. hw_mode = cs->hw_mode; /* Save original settings */
  369. cs->hw_mode = mpc8xxx_spi_read_reg(&reg_base->mode);
  370. /* mask out bits we are going to set */
  371. cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
  372. | SPMODE_REV | SPMODE_LOOP);
  373. if (spi->mode & SPI_CPHA)
  374. cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
  375. if (spi->mode & SPI_CPOL)
  376. cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
  377. if (!(spi->mode & SPI_LSB_FIRST))
  378. cs->hw_mode |= SPMODE_REV;
  379. if (spi->mode & SPI_LOOP)
  380. cs->hw_mode |= SPMODE_LOOP;
  381. retval = fsl_spi_setup_transfer(spi, NULL);
  382. if (retval < 0) {
  383. cs->hw_mode = hw_mode; /* Restore settings */
  384. return retval;
  385. }
  386. if (mpc8xxx_spi->type == TYPE_GRLIB) {
  387. if (gpio_is_valid(spi->cs_gpio)) {
  388. int desel;
  389. retval = gpio_request(spi->cs_gpio,
  390. dev_name(&spi->dev));
  391. if (retval)
  392. return retval;
  393. desel = !(spi->mode & SPI_CS_HIGH);
  394. retval = gpio_direction_output(spi->cs_gpio, desel);
  395. if (retval) {
  396. gpio_free(spi->cs_gpio);
  397. return retval;
  398. }
  399. } else if (spi->cs_gpio != -ENOENT) {
  400. if (spi->cs_gpio < 0)
  401. return spi->cs_gpio;
  402. return -EINVAL;
  403. }
  404. /* When spi->cs_gpio == -ENOENT, a hole in the phandle list
  405. * indicates to use native chipselect if present, or allow for
  406. * an always selected chip
  407. */
  408. }
  409. /* Initialize chipselect - might be active for SPI_CS_HIGH mode */
  410. fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  411. return 0;
  412. }
  413. static void fsl_spi_cleanup(struct spi_device *spi)
  414. {
  415. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  416. if (mpc8xxx_spi->type == TYPE_GRLIB && gpio_is_valid(spi->cs_gpio))
  417. gpio_free(spi->cs_gpio);
  418. }
  419. static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
  420. {
  421. struct fsl_spi_reg *reg_base = mspi->reg_base;
  422. /* We need handle RX first */
  423. if (events & SPIE_NE) {
  424. u32 rx_data = mpc8xxx_spi_read_reg(&reg_base->receive);
  425. if (mspi->rx)
  426. mspi->get_rx(rx_data, mspi);
  427. }
  428. if ((events & SPIE_NF) == 0)
  429. /* spin until TX is done */
  430. while (((events =
  431. mpc8xxx_spi_read_reg(&reg_base->event)) &
  432. SPIE_NF) == 0)
  433. cpu_relax();
  434. /* Clear the events */
  435. mpc8xxx_spi_write_reg(&reg_base->event, events);
  436. mspi->count -= 1;
  437. if (mspi->count) {
  438. u32 word = mspi->get_tx(mspi);
  439. mpc8xxx_spi_write_reg(&reg_base->transmit, word);
  440. } else {
  441. complete(&mspi->done);
  442. }
  443. }
  444. static irqreturn_t fsl_spi_irq(s32 irq, void *context_data)
  445. {
  446. struct mpc8xxx_spi *mspi = context_data;
  447. irqreturn_t ret = IRQ_NONE;
  448. u32 events;
  449. struct fsl_spi_reg *reg_base = mspi->reg_base;
  450. /* Get interrupt events(tx/rx) */
  451. events = mpc8xxx_spi_read_reg(&reg_base->event);
  452. if (events)
  453. ret = IRQ_HANDLED;
  454. dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
  455. if (mspi->flags & SPI_CPM_MODE)
  456. fsl_spi_cpm_irq(mspi, events);
  457. else
  458. fsl_spi_cpu_irq(mspi, events);
  459. return ret;
  460. }
  461. static void fsl_spi_remove(struct mpc8xxx_spi *mspi)
  462. {
  463. iounmap(mspi->reg_base);
  464. fsl_spi_cpm_free(mspi);
  465. }
  466. static void fsl_spi_grlib_cs_control(struct spi_device *spi, bool on)
  467. {
  468. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  469. struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base;
  470. u32 slvsel;
  471. u16 cs = spi->chip_select;
  472. if (gpio_is_valid(spi->cs_gpio)) {
  473. gpio_set_value(spi->cs_gpio, on);
  474. } else if (cs < mpc8xxx_spi->native_chipselects) {
  475. slvsel = mpc8xxx_spi_read_reg(&reg_base->slvsel);
  476. slvsel = on ? (slvsel | (1 << cs)) : (slvsel & ~(1 << cs));
  477. mpc8xxx_spi_write_reg(&reg_base->slvsel, slvsel);
  478. }
  479. }
  480. static void fsl_spi_grlib_probe(struct device *dev)
  481. {
  482. struct fsl_spi_platform_data *pdata = dev->platform_data;
  483. struct spi_master *master = dev_get_drvdata(dev);
  484. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
  485. struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base;
  486. int mbits;
  487. u32 capabilities;
  488. capabilities = mpc8xxx_spi_read_reg(&reg_base->cap);
  489. mpc8xxx_spi->set_shifts = fsl_spi_grlib_set_shifts;
  490. mbits = SPCAP_MAXWLEN(capabilities);
  491. if (mbits)
  492. mpc8xxx_spi->max_bits_per_word = mbits + 1;
  493. mpc8xxx_spi->native_chipselects = 0;
  494. if (SPCAP_SSEN(capabilities)) {
  495. mpc8xxx_spi->native_chipselects = SPCAP_SSSZ(capabilities);
  496. mpc8xxx_spi_write_reg(&reg_base->slvsel, 0xffffffff);
  497. }
  498. master->num_chipselect = mpc8xxx_spi->native_chipselects;
  499. pdata->cs_control = fsl_spi_grlib_cs_control;
  500. }
  501. static struct spi_master * fsl_spi_probe(struct device *dev,
  502. struct resource *mem, unsigned int irq)
  503. {
  504. struct fsl_spi_platform_data *pdata = dev->platform_data;
  505. struct spi_master *master;
  506. struct mpc8xxx_spi *mpc8xxx_spi;
  507. struct fsl_spi_reg *reg_base;
  508. u32 regval;
  509. int ret = 0;
  510. master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
  511. if (master == NULL) {
  512. ret = -ENOMEM;
  513. goto err;
  514. }
  515. dev_set_drvdata(dev, master);
  516. ret = mpc8xxx_spi_probe(dev, mem, irq);
  517. if (ret)
  518. goto err_probe;
  519. master->setup = fsl_spi_setup;
  520. master->cleanup = fsl_spi_cleanup;
  521. mpc8xxx_spi = spi_master_get_devdata(master);
  522. mpc8xxx_spi->spi_do_one_msg = fsl_spi_do_one_msg;
  523. mpc8xxx_spi->spi_remove = fsl_spi_remove;
  524. mpc8xxx_spi->max_bits_per_word = 32;
  525. mpc8xxx_spi->type = fsl_spi_get_type(dev);
  526. ret = fsl_spi_cpm_init(mpc8xxx_spi);
  527. if (ret)
  528. goto err_cpm_init;
  529. mpc8xxx_spi->reg_base = ioremap(mem->start, resource_size(mem));
  530. if (mpc8xxx_spi->reg_base == NULL) {
  531. ret = -ENOMEM;
  532. goto err_ioremap;
  533. }
  534. if (mpc8xxx_spi->type == TYPE_GRLIB)
  535. fsl_spi_grlib_probe(dev);
  536. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
  537. mpc8xxx_spi->set_shifts = fsl_spi_qe_cpu_set_shifts;
  538. if (mpc8xxx_spi->set_shifts)
  539. /* 8 bits per word and MSB first */
  540. mpc8xxx_spi->set_shifts(&mpc8xxx_spi->rx_shift,
  541. &mpc8xxx_spi->tx_shift, 8, 1);
  542. /* Register for SPI Interrupt */
  543. ret = request_irq(mpc8xxx_spi->irq, fsl_spi_irq,
  544. 0, "fsl_spi", mpc8xxx_spi);
  545. if (ret != 0)
  546. goto free_irq;
  547. reg_base = mpc8xxx_spi->reg_base;
  548. /* SPI controller initializations */
  549. mpc8xxx_spi_write_reg(&reg_base->mode, 0);
  550. mpc8xxx_spi_write_reg(&reg_base->mask, 0);
  551. mpc8xxx_spi_write_reg(&reg_base->command, 0);
  552. mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff);
  553. /* Enable SPI interface */
  554. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  555. if (mpc8xxx_spi->max_bits_per_word < 8) {
  556. regval &= ~SPMODE_LEN(0xF);
  557. regval |= SPMODE_LEN(mpc8xxx_spi->max_bits_per_word - 1);
  558. }
  559. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
  560. regval |= SPMODE_OP;
  561. mpc8xxx_spi_write_reg(&reg_base->mode, regval);
  562. ret = spi_register_master(master);
  563. if (ret < 0)
  564. goto unreg_master;
  565. dev_info(dev, "at 0x%p (irq = %d), %s mode\n", reg_base,
  566. mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
  567. return master;
  568. unreg_master:
  569. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  570. free_irq:
  571. iounmap(mpc8xxx_spi->reg_base);
  572. err_ioremap:
  573. fsl_spi_cpm_free(mpc8xxx_spi);
  574. err_cpm_init:
  575. err_probe:
  576. spi_master_put(master);
  577. err:
  578. return ERR_PTR(ret);
  579. }
  580. static void fsl_spi_cs_control(struct spi_device *spi, bool on)
  581. {
  582. struct device *dev = spi->dev.parent->parent;
  583. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
  584. u16 cs = spi->chip_select;
  585. int gpio = pinfo->gpios[cs];
  586. bool alow = pinfo->alow_flags[cs];
  587. gpio_set_value(gpio, on ^ alow);
  588. }
  589. static int of_fsl_spi_get_chipselects(struct device *dev)
  590. {
  591. struct device_node *np = dev->of_node;
  592. struct fsl_spi_platform_data *pdata = dev->platform_data;
  593. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  594. int ngpios;
  595. int i = 0;
  596. int ret;
  597. ngpios = of_gpio_count(np);
  598. if (ngpios <= 0) {
  599. /*
  600. * SPI w/o chip-select line. One SPI device is still permitted
  601. * though.
  602. */
  603. pdata->max_chipselect = 1;
  604. return 0;
  605. }
  606. pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
  607. if (!pinfo->gpios)
  608. return -ENOMEM;
  609. memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
  610. pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
  611. GFP_KERNEL);
  612. if (!pinfo->alow_flags) {
  613. ret = -ENOMEM;
  614. goto err_alloc_flags;
  615. }
  616. for (; i < ngpios; i++) {
  617. int gpio;
  618. enum of_gpio_flags flags;
  619. gpio = of_get_gpio_flags(np, i, &flags);
  620. if (!gpio_is_valid(gpio)) {
  621. dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
  622. ret = gpio;
  623. goto err_loop;
  624. }
  625. ret = gpio_request(gpio, dev_name(dev));
  626. if (ret) {
  627. dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
  628. goto err_loop;
  629. }
  630. pinfo->gpios[i] = gpio;
  631. pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
  632. ret = gpio_direction_output(pinfo->gpios[i],
  633. pinfo->alow_flags[i]);
  634. if (ret) {
  635. dev_err(dev, "can't set output direction for gpio "
  636. "#%d: %d\n", i, ret);
  637. goto err_loop;
  638. }
  639. }
  640. pdata->max_chipselect = ngpios;
  641. pdata->cs_control = fsl_spi_cs_control;
  642. return 0;
  643. err_loop:
  644. while (i >= 0) {
  645. if (gpio_is_valid(pinfo->gpios[i]))
  646. gpio_free(pinfo->gpios[i]);
  647. i--;
  648. }
  649. kfree(pinfo->alow_flags);
  650. pinfo->alow_flags = NULL;
  651. err_alloc_flags:
  652. kfree(pinfo->gpios);
  653. pinfo->gpios = NULL;
  654. return ret;
  655. }
  656. static int of_fsl_spi_free_chipselects(struct device *dev)
  657. {
  658. struct fsl_spi_platform_data *pdata = dev->platform_data;
  659. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  660. int i;
  661. if (!pinfo->gpios)
  662. return 0;
  663. for (i = 0; i < pdata->max_chipselect; i++) {
  664. if (gpio_is_valid(pinfo->gpios[i]))
  665. gpio_free(pinfo->gpios[i]);
  666. }
  667. kfree(pinfo->gpios);
  668. kfree(pinfo->alow_flags);
  669. return 0;
  670. }
  671. static int of_fsl_spi_probe(struct platform_device *ofdev)
  672. {
  673. struct device *dev = &ofdev->dev;
  674. struct device_node *np = ofdev->dev.of_node;
  675. struct spi_master *master;
  676. struct resource mem;
  677. int irq, type;
  678. int ret = -ENOMEM;
  679. ret = of_mpc8xxx_spi_probe(ofdev);
  680. if (ret)
  681. return ret;
  682. type = fsl_spi_get_type(&ofdev->dev);
  683. if (type == TYPE_FSL) {
  684. ret = of_fsl_spi_get_chipselects(dev);
  685. if (ret)
  686. goto err;
  687. }
  688. ret = of_address_to_resource(np, 0, &mem);
  689. if (ret)
  690. goto err;
  691. irq = irq_of_parse_and_map(np, 0);
  692. if (!irq) {
  693. ret = -EINVAL;
  694. goto err;
  695. }
  696. master = fsl_spi_probe(dev, &mem, irq);
  697. if (IS_ERR(master)) {
  698. ret = PTR_ERR(master);
  699. goto err;
  700. }
  701. return 0;
  702. err:
  703. if (type == TYPE_FSL)
  704. of_fsl_spi_free_chipselects(dev);
  705. return ret;
  706. }
  707. static int of_fsl_spi_remove(struct platform_device *ofdev)
  708. {
  709. struct spi_master *master = platform_get_drvdata(ofdev);
  710. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
  711. int ret;
  712. ret = mpc8xxx_spi_remove(&ofdev->dev);
  713. if (ret)
  714. return ret;
  715. if (mpc8xxx_spi->type == TYPE_FSL)
  716. of_fsl_spi_free_chipselects(&ofdev->dev);
  717. return 0;
  718. }
  719. static struct platform_driver of_fsl_spi_driver = {
  720. .driver = {
  721. .name = "fsl_spi",
  722. .owner = THIS_MODULE,
  723. .of_match_table = of_fsl_spi_match,
  724. },
  725. .probe = of_fsl_spi_probe,
  726. .remove = of_fsl_spi_remove,
  727. };
  728. #ifdef CONFIG_MPC832x_RDB
  729. /*
  730. * XXX XXX XXX
  731. * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
  732. * only. The driver should go away soon, since newer MPC8323E-RDB's device
  733. * tree can work with OpenFirmware driver. But for now we support old trees
  734. * as well.
  735. */
  736. static int plat_mpc8xxx_spi_probe(struct platform_device *pdev)
  737. {
  738. struct resource *mem;
  739. int irq;
  740. struct spi_master *master;
  741. if (!pdev->dev.platform_data)
  742. return -EINVAL;
  743. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  744. if (!mem)
  745. return -EINVAL;
  746. irq = platform_get_irq(pdev, 0);
  747. if (irq <= 0)
  748. return -EINVAL;
  749. master = fsl_spi_probe(&pdev->dev, mem, irq);
  750. return PTR_ERR_OR_ZERO(master);
  751. }
  752. static int plat_mpc8xxx_spi_remove(struct platform_device *pdev)
  753. {
  754. return mpc8xxx_spi_remove(&pdev->dev);
  755. }
  756. MODULE_ALIAS("platform:mpc8xxx_spi");
  757. static struct platform_driver mpc8xxx_spi_driver = {
  758. .probe = plat_mpc8xxx_spi_probe,
  759. .remove = plat_mpc8xxx_spi_remove,
  760. .driver = {
  761. .name = "mpc8xxx_spi",
  762. .owner = THIS_MODULE,
  763. },
  764. };
  765. static bool legacy_driver_failed;
  766. static void __init legacy_driver_register(void)
  767. {
  768. legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
  769. }
  770. static void __exit legacy_driver_unregister(void)
  771. {
  772. if (legacy_driver_failed)
  773. return;
  774. platform_driver_unregister(&mpc8xxx_spi_driver);
  775. }
  776. #else
  777. static void __init legacy_driver_register(void) {}
  778. static void __exit legacy_driver_unregister(void) {}
  779. #endif /* CONFIG_MPC832x_RDB */
  780. static int __init fsl_spi_init(void)
  781. {
  782. legacy_driver_register();
  783. return platform_driver_register(&of_fsl_spi_driver);
  784. }
  785. module_init(fsl_spi_init);
  786. static void __exit fsl_spi_exit(void)
  787. {
  788. platform_driver_unregister(&of_fsl_spi_driver);
  789. legacy_driver_unregister();
  790. }
  791. module_exit(fsl_spi_exit);
  792. MODULE_AUTHOR("Kumar Gala");
  793. MODULE_DESCRIPTION("Simple Freescale SPI Driver");
  794. MODULE_LICENSE("GPL");