spi-pxa2xx.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  1. /*
  2. * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
  3. * Copyright (C) 2013, Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/bitops.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/device.h>
  19. #include <linux/ioport.h>
  20. #include <linux/errno.h>
  21. #include <linux/err.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/kernel.h>
  24. #include <linux/pci.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/spi/pxa2xx_spi.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/delay.h>
  29. #include <linux/gpio.h>
  30. #include <linux/gpio/consumer.h>
  31. #include <linux/slab.h>
  32. #include <linux/clk.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/acpi.h>
  35. #include "spi-pxa2xx.h"
  36. MODULE_AUTHOR("Stephen Street");
  37. MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
  38. MODULE_LICENSE("GPL");
  39. MODULE_ALIAS("platform:pxa2xx-spi");
  40. #define TIMOUT_DFLT 1000
  41. /*
  42. * for testing SSCR1 changes that require SSP restart, basically
  43. * everything except the service and interrupt enables, the pxa270 developer
  44. * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
  45. * list, but the PXA255 dev man says all bits without really meaning the
  46. * service and interrupt enables
  47. */
  48. #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
  49. | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
  50. | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
  51. | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
  52. | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
  53. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  54. #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
  55. | QUARK_X1000_SSCR1_EFWR \
  56. | QUARK_X1000_SSCR1_RFT \
  57. | QUARK_X1000_SSCR1_TFT \
  58. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  59. #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
  60. | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
  61. | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
  62. | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
  63. | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
  64. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  65. #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
  66. #define LPSS_CS_CONTROL_SW_MODE BIT(0)
  67. #define LPSS_CS_CONTROL_CS_HIGH BIT(1)
  68. #define LPSS_CAPS_CS_EN_SHIFT 9
  69. #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT)
  70. struct lpss_config {
  71. /* LPSS offset from drv_data->ioaddr */
  72. unsigned offset;
  73. /* Register offsets from drv_data->lpss_base or -1 */
  74. int reg_general;
  75. int reg_ssp;
  76. int reg_cs_ctrl;
  77. int reg_capabilities;
  78. /* FIFO thresholds */
  79. u32 rx_threshold;
  80. u32 tx_threshold_lo;
  81. u32 tx_threshold_hi;
  82. /* Chip select control */
  83. unsigned cs_sel_shift;
  84. unsigned cs_sel_mask;
  85. unsigned cs_num;
  86. };
  87. /* Keep these sorted with enum pxa_ssp_type */
  88. static const struct lpss_config lpss_platforms[] = {
  89. { /* LPSS_LPT_SSP */
  90. .offset = 0x800,
  91. .reg_general = 0x08,
  92. .reg_ssp = 0x0c,
  93. .reg_cs_ctrl = 0x18,
  94. .reg_capabilities = -1,
  95. .rx_threshold = 64,
  96. .tx_threshold_lo = 160,
  97. .tx_threshold_hi = 224,
  98. },
  99. { /* LPSS_BYT_SSP */
  100. .offset = 0x400,
  101. .reg_general = 0x08,
  102. .reg_ssp = 0x0c,
  103. .reg_cs_ctrl = 0x18,
  104. .reg_capabilities = -1,
  105. .rx_threshold = 64,
  106. .tx_threshold_lo = 160,
  107. .tx_threshold_hi = 224,
  108. },
  109. { /* LPSS_BSW_SSP */
  110. .offset = 0x400,
  111. .reg_general = 0x08,
  112. .reg_ssp = 0x0c,
  113. .reg_cs_ctrl = 0x18,
  114. .reg_capabilities = -1,
  115. .rx_threshold = 64,
  116. .tx_threshold_lo = 160,
  117. .tx_threshold_hi = 224,
  118. .cs_sel_shift = 2,
  119. .cs_sel_mask = 1 << 2,
  120. .cs_num = 2,
  121. },
  122. { /* LPSS_SPT_SSP */
  123. .offset = 0x200,
  124. .reg_general = -1,
  125. .reg_ssp = 0x20,
  126. .reg_cs_ctrl = 0x24,
  127. .reg_capabilities = -1,
  128. .rx_threshold = 1,
  129. .tx_threshold_lo = 32,
  130. .tx_threshold_hi = 56,
  131. },
  132. { /* LPSS_BXT_SSP */
  133. .offset = 0x200,
  134. .reg_general = -1,
  135. .reg_ssp = 0x20,
  136. .reg_cs_ctrl = 0x24,
  137. .reg_capabilities = 0xfc,
  138. .rx_threshold = 1,
  139. .tx_threshold_lo = 16,
  140. .tx_threshold_hi = 48,
  141. .cs_sel_shift = 8,
  142. .cs_sel_mask = 3 << 8,
  143. },
  144. };
  145. static inline const struct lpss_config
  146. *lpss_get_config(const struct driver_data *drv_data)
  147. {
  148. return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
  149. }
  150. static bool is_lpss_ssp(const struct driver_data *drv_data)
  151. {
  152. switch (drv_data->ssp_type) {
  153. case LPSS_LPT_SSP:
  154. case LPSS_BYT_SSP:
  155. case LPSS_BSW_SSP:
  156. case LPSS_SPT_SSP:
  157. case LPSS_BXT_SSP:
  158. return true;
  159. default:
  160. return false;
  161. }
  162. }
  163. static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
  164. {
  165. return drv_data->ssp_type == QUARK_X1000_SSP;
  166. }
  167. static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
  168. {
  169. switch (drv_data->ssp_type) {
  170. case QUARK_X1000_SSP:
  171. return QUARK_X1000_SSCR1_CHANGE_MASK;
  172. case CE4100_SSP:
  173. return CE4100_SSCR1_CHANGE_MASK;
  174. default:
  175. return SSCR1_CHANGE_MASK;
  176. }
  177. }
  178. static u32
  179. pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
  180. {
  181. switch (drv_data->ssp_type) {
  182. case QUARK_X1000_SSP:
  183. return RX_THRESH_QUARK_X1000_DFLT;
  184. case CE4100_SSP:
  185. return RX_THRESH_CE4100_DFLT;
  186. default:
  187. return RX_THRESH_DFLT;
  188. }
  189. }
  190. static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
  191. {
  192. u32 mask;
  193. switch (drv_data->ssp_type) {
  194. case QUARK_X1000_SSP:
  195. mask = QUARK_X1000_SSSR_TFL_MASK;
  196. break;
  197. case CE4100_SSP:
  198. mask = CE4100_SSSR_TFL_MASK;
  199. break;
  200. default:
  201. mask = SSSR_TFL_MASK;
  202. break;
  203. }
  204. return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
  205. }
  206. static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
  207. u32 *sccr1_reg)
  208. {
  209. u32 mask;
  210. switch (drv_data->ssp_type) {
  211. case QUARK_X1000_SSP:
  212. mask = QUARK_X1000_SSCR1_RFT;
  213. break;
  214. case CE4100_SSP:
  215. mask = CE4100_SSCR1_RFT;
  216. break;
  217. default:
  218. mask = SSCR1_RFT;
  219. break;
  220. }
  221. *sccr1_reg &= ~mask;
  222. }
  223. static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
  224. u32 *sccr1_reg, u32 threshold)
  225. {
  226. switch (drv_data->ssp_type) {
  227. case QUARK_X1000_SSP:
  228. *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
  229. break;
  230. case CE4100_SSP:
  231. *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
  232. break;
  233. default:
  234. *sccr1_reg |= SSCR1_RxTresh(threshold);
  235. break;
  236. }
  237. }
  238. static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
  239. u32 clk_div, u8 bits)
  240. {
  241. switch (drv_data->ssp_type) {
  242. case QUARK_X1000_SSP:
  243. return clk_div
  244. | QUARK_X1000_SSCR0_Motorola
  245. | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
  246. | SSCR0_SSE;
  247. default:
  248. return clk_div
  249. | SSCR0_Motorola
  250. | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
  251. | SSCR0_SSE
  252. | (bits > 16 ? SSCR0_EDSS : 0);
  253. }
  254. }
  255. /*
  256. * Read and write LPSS SSP private registers. Caller must first check that
  257. * is_lpss_ssp() returns true before these can be called.
  258. */
  259. static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
  260. {
  261. WARN_ON(!drv_data->lpss_base);
  262. return readl(drv_data->lpss_base + offset);
  263. }
  264. static void __lpss_ssp_write_priv(struct driver_data *drv_data,
  265. unsigned offset, u32 value)
  266. {
  267. WARN_ON(!drv_data->lpss_base);
  268. writel(value, drv_data->lpss_base + offset);
  269. }
  270. /*
  271. * lpss_ssp_setup - perform LPSS SSP specific setup
  272. * @drv_data: pointer to the driver private data
  273. *
  274. * Perform LPSS SSP specific setup. This function must be called first if
  275. * one is going to use LPSS SSP private registers.
  276. */
  277. static void lpss_ssp_setup(struct driver_data *drv_data)
  278. {
  279. const struct lpss_config *config;
  280. u32 value;
  281. config = lpss_get_config(drv_data);
  282. drv_data->lpss_base = drv_data->ioaddr + config->offset;
  283. /* Enable software chip select control */
  284. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  285. value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
  286. value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
  287. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  288. /* Enable multiblock DMA transfers */
  289. if (drv_data->master_info->enable_dma) {
  290. __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
  291. if (config->reg_general >= 0) {
  292. value = __lpss_ssp_read_priv(drv_data,
  293. config->reg_general);
  294. value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
  295. __lpss_ssp_write_priv(drv_data,
  296. config->reg_general, value);
  297. }
  298. }
  299. }
  300. static void lpss_ssp_select_cs(struct driver_data *drv_data,
  301. const struct lpss_config *config)
  302. {
  303. u32 value, cs;
  304. if (!config->cs_sel_mask)
  305. return;
  306. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  307. cs = drv_data->master->cur_msg->spi->chip_select;
  308. cs <<= config->cs_sel_shift;
  309. if (cs != (value & config->cs_sel_mask)) {
  310. /*
  311. * When switching another chip select output active the
  312. * output must be selected first and wait 2 ssp_clk cycles
  313. * before changing state to active. Otherwise a short
  314. * glitch will occur on the previous chip select since
  315. * output select is latched but state control is not.
  316. */
  317. value &= ~config->cs_sel_mask;
  318. value |= cs;
  319. __lpss_ssp_write_priv(drv_data,
  320. config->reg_cs_ctrl, value);
  321. ndelay(1000000000 /
  322. (drv_data->master->max_speed_hz / 2));
  323. }
  324. }
  325. static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
  326. {
  327. const struct lpss_config *config;
  328. u32 value;
  329. config = lpss_get_config(drv_data);
  330. if (enable)
  331. lpss_ssp_select_cs(drv_data, config);
  332. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  333. if (enable)
  334. value &= ~LPSS_CS_CONTROL_CS_HIGH;
  335. else
  336. value |= LPSS_CS_CONTROL_CS_HIGH;
  337. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  338. }
  339. static void cs_assert(struct driver_data *drv_data)
  340. {
  341. struct chip_data *chip =
  342. spi_get_ctldata(drv_data->master->cur_msg->spi);
  343. if (drv_data->ssp_type == CE4100_SSP) {
  344. pxa2xx_spi_write(drv_data, SSSR, chip->frm);
  345. return;
  346. }
  347. if (chip->cs_control) {
  348. chip->cs_control(PXA2XX_CS_ASSERT);
  349. return;
  350. }
  351. if (gpio_is_valid(chip->gpio_cs)) {
  352. gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
  353. return;
  354. }
  355. if (is_lpss_ssp(drv_data))
  356. lpss_ssp_cs_control(drv_data, true);
  357. }
  358. static void cs_deassert(struct driver_data *drv_data)
  359. {
  360. struct chip_data *chip =
  361. spi_get_ctldata(drv_data->master->cur_msg->spi);
  362. if (drv_data->ssp_type == CE4100_SSP)
  363. return;
  364. if (chip->cs_control) {
  365. chip->cs_control(PXA2XX_CS_DEASSERT);
  366. return;
  367. }
  368. if (gpio_is_valid(chip->gpio_cs)) {
  369. gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
  370. return;
  371. }
  372. if (is_lpss_ssp(drv_data))
  373. lpss_ssp_cs_control(drv_data, false);
  374. }
  375. int pxa2xx_spi_flush(struct driver_data *drv_data)
  376. {
  377. unsigned long limit = loops_per_jiffy << 1;
  378. do {
  379. while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  380. pxa2xx_spi_read(drv_data, SSDR);
  381. } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
  382. write_SSSR_CS(drv_data, SSSR_ROR);
  383. return limit;
  384. }
  385. static int null_writer(struct driver_data *drv_data)
  386. {
  387. u8 n_bytes = drv_data->n_bytes;
  388. if (pxa2xx_spi_txfifo_full(drv_data)
  389. || (drv_data->tx == drv_data->tx_end))
  390. return 0;
  391. pxa2xx_spi_write(drv_data, SSDR, 0);
  392. drv_data->tx += n_bytes;
  393. return 1;
  394. }
  395. static int null_reader(struct driver_data *drv_data)
  396. {
  397. u8 n_bytes = drv_data->n_bytes;
  398. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  399. && (drv_data->rx < drv_data->rx_end)) {
  400. pxa2xx_spi_read(drv_data, SSDR);
  401. drv_data->rx += n_bytes;
  402. }
  403. return drv_data->rx == drv_data->rx_end;
  404. }
  405. static int u8_writer(struct driver_data *drv_data)
  406. {
  407. if (pxa2xx_spi_txfifo_full(drv_data)
  408. || (drv_data->tx == drv_data->tx_end))
  409. return 0;
  410. pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
  411. ++drv_data->tx;
  412. return 1;
  413. }
  414. static int u8_reader(struct driver_data *drv_data)
  415. {
  416. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  417. && (drv_data->rx < drv_data->rx_end)) {
  418. *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  419. ++drv_data->rx;
  420. }
  421. return drv_data->rx == drv_data->rx_end;
  422. }
  423. static int u16_writer(struct driver_data *drv_data)
  424. {
  425. if (pxa2xx_spi_txfifo_full(drv_data)
  426. || (drv_data->tx == drv_data->tx_end))
  427. return 0;
  428. pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
  429. drv_data->tx += 2;
  430. return 1;
  431. }
  432. static int u16_reader(struct driver_data *drv_data)
  433. {
  434. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  435. && (drv_data->rx < drv_data->rx_end)) {
  436. *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  437. drv_data->rx += 2;
  438. }
  439. return drv_data->rx == drv_data->rx_end;
  440. }
  441. static int u32_writer(struct driver_data *drv_data)
  442. {
  443. if (pxa2xx_spi_txfifo_full(drv_data)
  444. || (drv_data->tx == drv_data->tx_end))
  445. return 0;
  446. pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
  447. drv_data->tx += 4;
  448. return 1;
  449. }
  450. static int u32_reader(struct driver_data *drv_data)
  451. {
  452. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  453. && (drv_data->rx < drv_data->rx_end)) {
  454. *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  455. drv_data->rx += 4;
  456. }
  457. return drv_data->rx == drv_data->rx_end;
  458. }
  459. void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
  460. {
  461. struct spi_message *msg = drv_data->master->cur_msg;
  462. struct spi_transfer *trans = drv_data->cur_transfer;
  463. /* Move to next transfer */
  464. if (trans->transfer_list.next != &msg->transfers) {
  465. drv_data->cur_transfer =
  466. list_entry(trans->transfer_list.next,
  467. struct spi_transfer,
  468. transfer_list);
  469. return RUNNING_STATE;
  470. } else
  471. return DONE_STATE;
  472. }
  473. /* caller already set message->status; dma and pio irqs are blocked */
  474. static void giveback(struct driver_data *drv_data)
  475. {
  476. struct spi_transfer* last_transfer;
  477. struct spi_message *msg;
  478. unsigned long timeout;
  479. msg = drv_data->master->cur_msg;
  480. drv_data->cur_transfer = NULL;
  481. last_transfer = list_last_entry(&msg->transfers, struct spi_transfer,
  482. transfer_list);
  483. /* Delay if requested before any change in chip select */
  484. if (last_transfer->delay_usecs)
  485. udelay(last_transfer->delay_usecs);
  486. /* Wait until SSP becomes idle before deasserting the CS */
  487. timeout = jiffies + msecs_to_jiffies(10);
  488. while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
  489. !time_after(jiffies, timeout))
  490. cpu_relax();
  491. /* Drop chip select UNLESS cs_change is true or we are returning
  492. * a message with an error, or next message is for another chip
  493. */
  494. if (!last_transfer->cs_change)
  495. cs_deassert(drv_data);
  496. else {
  497. struct spi_message *next_msg;
  498. /* Holding of cs was hinted, but we need to make sure
  499. * the next message is for the same chip. Don't waste
  500. * time with the following tests unless this was hinted.
  501. *
  502. * We cannot postpone this until pump_messages, because
  503. * after calling msg->complete (below) the driver that
  504. * sent the current message could be unloaded, which
  505. * could invalidate the cs_control() callback...
  506. */
  507. /* get a pointer to the next message, if any */
  508. next_msg = spi_get_next_queued_message(drv_data->master);
  509. /* see if the next and current messages point
  510. * to the same chip
  511. */
  512. if ((next_msg && next_msg->spi != msg->spi) ||
  513. msg->state == ERROR_STATE)
  514. cs_deassert(drv_data);
  515. }
  516. spi_finalize_current_message(drv_data->master);
  517. }
  518. static void reset_sccr1(struct driver_data *drv_data)
  519. {
  520. struct chip_data *chip =
  521. spi_get_ctldata(drv_data->master->cur_msg->spi);
  522. u32 sccr1_reg;
  523. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
  524. switch (drv_data->ssp_type) {
  525. case QUARK_X1000_SSP:
  526. sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
  527. break;
  528. case CE4100_SSP:
  529. sccr1_reg &= ~CE4100_SSCR1_RFT;
  530. break;
  531. default:
  532. sccr1_reg &= ~SSCR1_RFT;
  533. break;
  534. }
  535. sccr1_reg |= chip->threshold;
  536. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  537. }
  538. static void int_error_stop(struct driver_data *drv_data, const char* msg)
  539. {
  540. /* Stop and reset SSP */
  541. write_SSSR_CS(drv_data, drv_data->clear_sr);
  542. reset_sccr1(drv_data);
  543. if (!pxa25x_ssp_comp(drv_data))
  544. pxa2xx_spi_write(drv_data, SSTO, 0);
  545. pxa2xx_spi_flush(drv_data);
  546. pxa2xx_spi_write(drv_data, SSCR0,
  547. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  548. dev_err(&drv_data->pdev->dev, "%s\n", msg);
  549. drv_data->master->cur_msg->state = ERROR_STATE;
  550. tasklet_schedule(&drv_data->pump_transfers);
  551. }
  552. static void int_transfer_complete(struct driver_data *drv_data)
  553. {
  554. /* Clear and disable interrupts */
  555. write_SSSR_CS(drv_data, drv_data->clear_sr);
  556. reset_sccr1(drv_data);
  557. if (!pxa25x_ssp_comp(drv_data))
  558. pxa2xx_spi_write(drv_data, SSTO, 0);
  559. /* Update total byte transferred return count actual bytes read */
  560. drv_data->master->cur_msg->actual_length += drv_data->len -
  561. (drv_data->rx_end - drv_data->rx);
  562. /* Transfer delays and chip select release are
  563. * handled in pump_transfers or giveback
  564. */
  565. /* Move to next transfer */
  566. drv_data->master->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
  567. /* Schedule transfer tasklet */
  568. tasklet_schedule(&drv_data->pump_transfers);
  569. }
  570. static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
  571. {
  572. u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
  573. drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
  574. u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
  575. if (irq_status & SSSR_ROR) {
  576. int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
  577. return IRQ_HANDLED;
  578. }
  579. if (irq_status & SSSR_TINT) {
  580. pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
  581. if (drv_data->read(drv_data)) {
  582. int_transfer_complete(drv_data);
  583. return IRQ_HANDLED;
  584. }
  585. }
  586. /* Drain rx fifo, Fill tx fifo and prevent overruns */
  587. do {
  588. if (drv_data->read(drv_data)) {
  589. int_transfer_complete(drv_data);
  590. return IRQ_HANDLED;
  591. }
  592. } while (drv_data->write(drv_data));
  593. if (drv_data->read(drv_data)) {
  594. int_transfer_complete(drv_data);
  595. return IRQ_HANDLED;
  596. }
  597. if (drv_data->tx == drv_data->tx_end) {
  598. u32 bytes_left;
  599. u32 sccr1_reg;
  600. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  601. sccr1_reg &= ~SSCR1_TIE;
  602. /*
  603. * PXA25x_SSP has no timeout, set up rx threshould for the
  604. * remaining RX bytes.
  605. */
  606. if (pxa25x_ssp_comp(drv_data)) {
  607. u32 rx_thre;
  608. pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
  609. bytes_left = drv_data->rx_end - drv_data->rx;
  610. switch (drv_data->n_bytes) {
  611. case 4:
  612. bytes_left >>= 1;
  613. case 2:
  614. bytes_left >>= 1;
  615. }
  616. rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
  617. if (rx_thre > bytes_left)
  618. rx_thre = bytes_left;
  619. pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
  620. }
  621. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  622. }
  623. /* We did something */
  624. return IRQ_HANDLED;
  625. }
  626. static void handle_bad_msg(struct driver_data *drv_data)
  627. {
  628. pxa2xx_spi_write(drv_data, SSCR0,
  629. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  630. pxa2xx_spi_write(drv_data, SSCR1,
  631. pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1);
  632. if (!pxa25x_ssp_comp(drv_data))
  633. pxa2xx_spi_write(drv_data, SSTO, 0);
  634. write_SSSR_CS(drv_data, drv_data->clear_sr);
  635. dev_err(&drv_data->pdev->dev,
  636. "bad message state in interrupt handler\n");
  637. }
  638. static irqreturn_t ssp_int(int irq, void *dev_id)
  639. {
  640. struct driver_data *drv_data = dev_id;
  641. u32 sccr1_reg;
  642. u32 mask = drv_data->mask_sr;
  643. u32 status;
  644. /*
  645. * The IRQ might be shared with other peripherals so we must first
  646. * check that are we RPM suspended or not. If we are we assume that
  647. * the IRQ was not for us (we shouldn't be RPM suspended when the
  648. * interrupt is enabled).
  649. */
  650. if (pm_runtime_suspended(&drv_data->pdev->dev))
  651. return IRQ_NONE;
  652. /*
  653. * If the device is not yet in RPM suspended state and we get an
  654. * interrupt that is meant for another device, check if status bits
  655. * are all set to one. That means that the device is already
  656. * powered off.
  657. */
  658. status = pxa2xx_spi_read(drv_data, SSSR);
  659. if (status == ~0)
  660. return IRQ_NONE;
  661. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  662. /* Ignore possible writes if we don't need to write */
  663. if (!(sccr1_reg & SSCR1_TIE))
  664. mask &= ~SSSR_TFS;
  665. /* Ignore RX timeout interrupt if it is disabled */
  666. if (!(sccr1_reg & SSCR1_TINTE))
  667. mask &= ~SSSR_TINT;
  668. if (!(status & mask))
  669. return IRQ_NONE;
  670. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
  671. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  672. if (!drv_data->master->cur_msg) {
  673. handle_bad_msg(drv_data);
  674. /* Never fail */
  675. return IRQ_HANDLED;
  676. }
  677. return drv_data->transfer_handler(drv_data);
  678. }
  679. /*
  680. * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
  681. * input frequency by fractions of 2^24. It also has a divider by 5.
  682. *
  683. * There are formulas to get baud rate value for given input frequency and
  684. * divider parameters, such as DDS_CLK_RATE and SCR:
  685. *
  686. * Fsys = 200MHz
  687. *
  688. * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
  689. * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
  690. *
  691. * DDS_CLK_RATE either 2^n or 2^n / 5.
  692. * SCR is in range 0 .. 255
  693. *
  694. * Divisor = 5^i * 2^j * 2 * k
  695. * i = [0, 1] i = 1 iff j = 0 or j > 3
  696. * j = [0, 23] j = 0 iff i = 1
  697. * k = [1, 256]
  698. * Special case: j = 0, i = 1: Divisor = 2 / 5
  699. *
  700. * Accordingly to the specification the recommended values for DDS_CLK_RATE
  701. * are:
  702. * Case 1: 2^n, n = [0, 23]
  703. * Case 2: 2^24 * 2 / 5 (0x666666)
  704. * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
  705. *
  706. * In all cases the lowest possible value is better.
  707. *
  708. * The function calculates parameters for all cases and chooses the one closest
  709. * to the asked baud rate.
  710. */
  711. static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
  712. {
  713. unsigned long xtal = 200000000;
  714. unsigned long fref = xtal / 2; /* mandatory division by 2,
  715. see (2) */
  716. /* case 3 */
  717. unsigned long fref1 = fref / 2; /* case 1 */
  718. unsigned long fref2 = fref * 2 / 5; /* case 2 */
  719. unsigned long scale;
  720. unsigned long q, q1, q2;
  721. long r, r1, r2;
  722. u32 mul;
  723. /* Case 1 */
  724. /* Set initial value for DDS_CLK_RATE */
  725. mul = (1 << 24) >> 1;
  726. /* Calculate initial quot */
  727. q1 = DIV_ROUND_UP(fref1, rate);
  728. /* Scale q1 if it's too big */
  729. if (q1 > 256) {
  730. /* Scale q1 to range [1, 512] */
  731. scale = fls_long(q1 - 1);
  732. if (scale > 9) {
  733. q1 >>= scale - 9;
  734. mul >>= scale - 9;
  735. }
  736. /* Round the result if we have a remainder */
  737. q1 += q1 & 1;
  738. }
  739. /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
  740. scale = __ffs(q1);
  741. q1 >>= scale;
  742. mul >>= scale;
  743. /* Get the remainder */
  744. r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
  745. /* Case 2 */
  746. q2 = DIV_ROUND_UP(fref2, rate);
  747. r2 = abs(fref2 / q2 - rate);
  748. /*
  749. * Choose the best between two: less remainder we have the better. We
  750. * can't go case 2 if q2 is greater than 256 since SCR register can
  751. * hold only values 0 .. 255.
  752. */
  753. if (r2 >= r1 || q2 > 256) {
  754. /* case 1 is better */
  755. r = r1;
  756. q = q1;
  757. } else {
  758. /* case 2 is better */
  759. r = r2;
  760. q = q2;
  761. mul = (1 << 24) * 2 / 5;
  762. }
  763. /* Check case 3 only if the divisor is big enough */
  764. if (fref / rate >= 80) {
  765. u64 fssp;
  766. u32 m;
  767. /* Calculate initial quot */
  768. q1 = DIV_ROUND_UP(fref, rate);
  769. m = (1 << 24) / q1;
  770. /* Get the remainder */
  771. fssp = (u64)fref * m;
  772. do_div(fssp, 1 << 24);
  773. r1 = abs(fssp - rate);
  774. /* Choose this one if it suits better */
  775. if (r1 < r) {
  776. /* case 3 is better */
  777. q = 1;
  778. mul = m;
  779. }
  780. }
  781. *dds = mul;
  782. return q - 1;
  783. }
  784. static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
  785. {
  786. unsigned long ssp_clk = drv_data->master->max_speed_hz;
  787. const struct ssp_device *ssp = drv_data->ssp;
  788. rate = min_t(int, ssp_clk, rate);
  789. if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
  790. return (ssp_clk / (2 * rate) - 1) & 0xff;
  791. else
  792. return (ssp_clk / rate - 1) & 0xfff;
  793. }
  794. static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
  795. int rate)
  796. {
  797. struct chip_data *chip =
  798. spi_get_ctldata(drv_data->master->cur_msg->spi);
  799. unsigned int clk_div;
  800. switch (drv_data->ssp_type) {
  801. case QUARK_X1000_SSP:
  802. clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
  803. break;
  804. default:
  805. clk_div = ssp_get_clk_div(drv_data, rate);
  806. break;
  807. }
  808. return clk_div << 8;
  809. }
  810. static bool pxa2xx_spi_can_dma(struct spi_master *master,
  811. struct spi_device *spi,
  812. struct spi_transfer *xfer)
  813. {
  814. struct chip_data *chip = spi_get_ctldata(spi);
  815. return chip->enable_dma &&
  816. xfer->len <= MAX_DMA_LEN &&
  817. xfer->len >= chip->dma_burst_size;
  818. }
  819. static void pump_transfers(unsigned long data)
  820. {
  821. struct driver_data *drv_data = (struct driver_data *)data;
  822. struct spi_master *master = drv_data->master;
  823. struct spi_message *message = master->cur_msg;
  824. struct chip_data *chip = spi_get_ctldata(message->spi);
  825. u32 dma_thresh = chip->dma_threshold;
  826. u32 dma_burst = chip->dma_burst_size;
  827. u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
  828. struct spi_transfer *transfer;
  829. struct spi_transfer *previous;
  830. u32 clk_div;
  831. u8 bits;
  832. u32 speed;
  833. u32 cr0;
  834. u32 cr1;
  835. int err;
  836. int dma_mapped;
  837. /* Get current state information */
  838. transfer = drv_data->cur_transfer;
  839. /* Handle for abort */
  840. if (message->state == ERROR_STATE) {
  841. message->status = -EIO;
  842. giveback(drv_data);
  843. return;
  844. }
  845. /* Handle end of message */
  846. if (message->state == DONE_STATE) {
  847. message->status = 0;
  848. giveback(drv_data);
  849. return;
  850. }
  851. /* Delay if requested at end of transfer before CS change */
  852. if (message->state == RUNNING_STATE) {
  853. previous = list_entry(transfer->transfer_list.prev,
  854. struct spi_transfer,
  855. transfer_list);
  856. if (previous->delay_usecs)
  857. udelay(previous->delay_usecs);
  858. /* Drop chip select only if cs_change is requested */
  859. if (previous->cs_change)
  860. cs_deassert(drv_data);
  861. }
  862. /* Check if we can DMA this transfer */
  863. if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
  864. /* reject already-mapped transfers; PIO won't always work */
  865. if (message->is_dma_mapped
  866. || transfer->rx_dma || transfer->tx_dma) {
  867. dev_err(&drv_data->pdev->dev,
  868. "pump_transfers: mapped transfer length of "
  869. "%u is greater than %d\n",
  870. transfer->len, MAX_DMA_LEN);
  871. message->status = -EINVAL;
  872. giveback(drv_data);
  873. return;
  874. }
  875. /* warn ... we force this to PIO mode */
  876. dev_warn_ratelimited(&message->spi->dev,
  877. "pump_transfers: DMA disabled for transfer length %ld "
  878. "greater than %d\n",
  879. (long)drv_data->len, MAX_DMA_LEN);
  880. }
  881. /* Setup the transfer state based on the type of transfer */
  882. if (pxa2xx_spi_flush(drv_data) == 0) {
  883. dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
  884. message->status = -EIO;
  885. giveback(drv_data);
  886. return;
  887. }
  888. drv_data->n_bytes = chip->n_bytes;
  889. drv_data->tx = (void *)transfer->tx_buf;
  890. drv_data->tx_end = drv_data->tx + transfer->len;
  891. drv_data->rx = transfer->rx_buf;
  892. drv_data->rx_end = drv_data->rx + transfer->len;
  893. drv_data->len = transfer->len;
  894. drv_data->write = drv_data->tx ? chip->write : null_writer;
  895. drv_data->read = drv_data->rx ? chip->read : null_reader;
  896. /* Change speed and bit per word on a per transfer */
  897. bits = transfer->bits_per_word;
  898. speed = transfer->speed_hz;
  899. clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
  900. if (bits <= 8) {
  901. drv_data->n_bytes = 1;
  902. drv_data->read = drv_data->read != null_reader ?
  903. u8_reader : null_reader;
  904. drv_data->write = drv_data->write != null_writer ?
  905. u8_writer : null_writer;
  906. } else if (bits <= 16) {
  907. drv_data->n_bytes = 2;
  908. drv_data->read = drv_data->read != null_reader ?
  909. u16_reader : null_reader;
  910. drv_data->write = drv_data->write != null_writer ?
  911. u16_writer : null_writer;
  912. } else if (bits <= 32) {
  913. drv_data->n_bytes = 4;
  914. drv_data->read = drv_data->read != null_reader ?
  915. u32_reader : null_reader;
  916. drv_data->write = drv_data->write != null_writer ?
  917. u32_writer : null_writer;
  918. }
  919. /*
  920. * if bits/word is changed in dma mode, then must check the
  921. * thresholds and burst also
  922. */
  923. if (chip->enable_dma) {
  924. if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
  925. message->spi,
  926. bits, &dma_burst,
  927. &dma_thresh))
  928. dev_warn_ratelimited(&message->spi->dev,
  929. "pump_transfers: DMA burst size reduced to match bits_per_word\n");
  930. }
  931. message->state = RUNNING_STATE;
  932. dma_mapped = master->can_dma &&
  933. master->can_dma(master, message->spi, transfer) &&
  934. master->cur_msg_mapped;
  935. if (dma_mapped) {
  936. /* Ensure we have the correct interrupt handler */
  937. drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
  938. err = pxa2xx_spi_dma_prepare(drv_data, dma_burst);
  939. if (err) {
  940. message->status = err;
  941. giveback(drv_data);
  942. return;
  943. }
  944. /* Clear status and start DMA engine */
  945. cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
  946. pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
  947. pxa2xx_spi_dma_start(drv_data);
  948. } else {
  949. /* Ensure we have the correct interrupt handler */
  950. drv_data->transfer_handler = interrupt_transfer;
  951. /* Clear status */
  952. cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
  953. write_SSSR_CS(drv_data, drv_data->clear_sr);
  954. }
  955. /* NOTE: PXA25x_SSP _could_ use external clocking ... */
  956. cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
  957. if (!pxa25x_ssp_comp(drv_data))
  958. dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
  959. master->max_speed_hz
  960. / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
  961. dma_mapped ? "DMA" : "PIO");
  962. else
  963. dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
  964. master->max_speed_hz / 2
  965. / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
  966. dma_mapped ? "DMA" : "PIO");
  967. if (is_lpss_ssp(drv_data)) {
  968. if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
  969. != chip->lpss_rx_threshold)
  970. pxa2xx_spi_write(drv_data, SSIRF,
  971. chip->lpss_rx_threshold);
  972. if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
  973. != chip->lpss_tx_threshold)
  974. pxa2xx_spi_write(drv_data, SSITF,
  975. chip->lpss_tx_threshold);
  976. }
  977. if (is_quark_x1000_ssp(drv_data) &&
  978. (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
  979. pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
  980. /* see if we need to reload the config registers */
  981. if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
  982. || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
  983. != (cr1 & change_mask)) {
  984. /* stop the SSP, and update the other bits */
  985. pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
  986. if (!pxa25x_ssp_comp(drv_data))
  987. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  988. /* first set CR1 without interrupt and service enables */
  989. pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
  990. /* restart the SSP */
  991. pxa2xx_spi_write(drv_data, SSCR0, cr0);
  992. } else {
  993. if (!pxa25x_ssp_comp(drv_data))
  994. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  995. }
  996. cs_assert(drv_data);
  997. /* after chip select, release the data by enabling service
  998. * requests and interrupts, without changing any mode bits */
  999. pxa2xx_spi_write(drv_data, SSCR1, cr1);
  1000. }
  1001. static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
  1002. struct spi_message *msg)
  1003. {
  1004. struct driver_data *drv_data = spi_master_get_devdata(master);
  1005. /* Initial message state*/
  1006. msg->state = START_STATE;
  1007. drv_data->cur_transfer = list_entry(msg->transfers.next,
  1008. struct spi_transfer,
  1009. transfer_list);
  1010. /* Mark as busy and launch transfers */
  1011. tasklet_schedule(&drv_data->pump_transfers);
  1012. return 0;
  1013. }
  1014. static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
  1015. {
  1016. struct driver_data *drv_data = spi_master_get_devdata(master);
  1017. /* Disable the SSP now */
  1018. pxa2xx_spi_write(drv_data, SSCR0,
  1019. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  1020. return 0;
  1021. }
  1022. static int setup_cs(struct spi_device *spi, struct chip_data *chip,
  1023. struct pxa2xx_spi_chip *chip_info)
  1024. {
  1025. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1026. int err = 0;
  1027. if (chip == NULL)
  1028. return 0;
  1029. if (drv_data->cs_gpiods) {
  1030. struct gpio_desc *gpiod;
  1031. gpiod = drv_data->cs_gpiods[spi->chip_select];
  1032. if (gpiod) {
  1033. chip->gpio_cs = desc_to_gpio(gpiod);
  1034. chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
  1035. gpiod_set_value(gpiod, chip->gpio_cs_inverted);
  1036. }
  1037. return 0;
  1038. }
  1039. if (chip_info == NULL)
  1040. return 0;
  1041. /* NOTE: setup() can be called multiple times, possibly with
  1042. * different chip_info, release previously requested GPIO
  1043. */
  1044. if (gpio_is_valid(chip->gpio_cs))
  1045. gpio_free(chip->gpio_cs);
  1046. /* If (*cs_control) is provided, ignore GPIO chip select */
  1047. if (chip_info->cs_control) {
  1048. chip->cs_control = chip_info->cs_control;
  1049. return 0;
  1050. }
  1051. if (gpio_is_valid(chip_info->gpio_cs)) {
  1052. err = gpio_request(chip_info->gpio_cs, "SPI_CS");
  1053. if (err) {
  1054. dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
  1055. chip_info->gpio_cs);
  1056. return err;
  1057. }
  1058. chip->gpio_cs = chip_info->gpio_cs;
  1059. chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
  1060. err = gpio_direction_output(chip->gpio_cs,
  1061. !chip->gpio_cs_inverted);
  1062. }
  1063. return err;
  1064. }
  1065. static int setup(struct spi_device *spi)
  1066. {
  1067. struct pxa2xx_spi_chip *chip_info;
  1068. struct chip_data *chip;
  1069. const struct lpss_config *config;
  1070. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1071. uint tx_thres, tx_hi_thres, rx_thres;
  1072. switch (drv_data->ssp_type) {
  1073. case QUARK_X1000_SSP:
  1074. tx_thres = TX_THRESH_QUARK_X1000_DFLT;
  1075. tx_hi_thres = 0;
  1076. rx_thres = RX_THRESH_QUARK_X1000_DFLT;
  1077. break;
  1078. case CE4100_SSP:
  1079. tx_thres = TX_THRESH_CE4100_DFLT;
  1080. tx_hi_thres = 0;
  1081. rx_thres = RX_THRESH_CE4100_DFLT;
  1082. break;
  1083. case LPSS_LPT_SSP:
  1084. case LPSS_BYT_SSP:
  1085. case LPSS_BSW_SSP:
  1086. case LPSS_SPT_SSP:
  1087. case LPSS_BXT_SSP:
  1088. config = lpss_get_config(drv_data);
  1089. tx_thres = config->tx_threshold_lo;
  1090. tx_hi_thres = config->tx_threshold_hi;
  1091. rx_thres = config->rx_threshold;
  1092. break;
  1093. default:
  1094. tx_thres = TX_THRESH_DFLT;
  1095. tx_hi_thres = 0;
  1096. rx_thres = RX_THRESH_DFLT;
  1097. break;
  1098. }
  1099. /* Only alloc on first setup */
  1100. chip = spi_get_ctldata(spi);
  1101. if (!chip) {
  1102. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  1103. if (!chip)
  1104. return -ENOMEM;
  1105. if (drv_data->ssp_type == CE4100_SSP) {
  1106. if (spi->chip_select > 4) {
  1107. dev_err(&spi->dev,
  1108. "failed setup: cs number must not be > 4.\n");
  1109. kfree(chip);
  1110. return -EINVAL;
  1111. }
  1112. chip->frm = spi->chip_select;
  1113. } else
  1114. chip->gpio_cs = -1;
  1115. chip->enable_dma = drv_data->master_info->enable_dma;
  1116. chip->timeout = TIMOUT_DFLT;
  1117. }
  1118. /* protocol drivers may change the chip settings, so...
  1119. * if chip_info exists, use it */
  1120. chip_info = spi->controller_data;
  1121. /* chip_info isn't always needed */
  1122. chip->cr1 = 0;
  1123. if (chip_info) {
  1124. if (chip_info->timeout)
  1125. chip->timeout = chip_info->timeout;
  1126. if (chip_info->tx_threshold)
  1127. tx_thres = chip_info->tx_threshold;
  1128. if (chip_info->tx_hi_threshold)
  1129. tx_hi_thres = chip_info->tx_hi_threshold;
  1130. if (chip_info->rx_threshold)
  1131. rx_thres = chip_info->rx_threshold;
  1132. chip->dma_threshold = 0;
  1133. if (chip_info->enable_loopback)
  1134. chip->cr1 = SSCR1_LBM;
  1135. }
  1136. chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
  1137. chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
  1138. | SSITF_TxHiThresh(tx_hi_thres);
  1139. /* set dma burst and threshold outside of chip_info path so that if
  1140. * chip_info goes away after setting chip->enable_dma, the
  1141. * burst and threshold can still respond to changes in bits_per_word */
  1142. if (chip->enable_dma) {
  1143. /* set up legal burst and threshold for dma */
  1144. if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
  1145. spi->bits_per_word,
  1146. &chip->dma_burst_size,
  1147. &chip->dma_threshold)) {
  1148. dev_warn(&spi->dev,
  1149. "in setup: DMA burst size reduced to match bits_per_word\n");
  1150. }
  1151. }
  1152. switch (drv_data->ssp_type) {
  1153. case QUARK_X1000_SSP:
  1154. chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
  1155. & QUARK_X1000_SSCR1_RFT)
  1156. | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
  1157. & QUARK_X1000_SSCR1_TFT);
  1158. break;
  1159. case CE4100_SSP:
  1160. chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
  1161. (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
  1162. break;
  1163. default:
  1164. chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
  1165. (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
  1166. break;
  1167. }
  1168. chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
  1169. chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
  1170. | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
  1171. if (spi->mode & SPI_LOOP)
  1172. chip->cr1 |= SSCR1_LBM;
  1173. if (spi->bits_per_word <= 8) {
  1174. chip->n_bytes = 1;
  1175. chip->read = u8_reader;
  1176. chip->write = u8_writer;
  1177. } else if (spi->bits_per_word <= 16) {
  1178. chip->n_bytes = 2;
  1179. chip->read = u16_reader;
  1180. chip->write = u16_writer;
  1181. } else if (spi->bits_per_word <= 32) {
  1182. chip->n_bytes = 4;
  1183. chip->read = u32_reader;
  1184. chip->write = u32_writer;
  1185. }
  1186. spi_set_ctldata(spi, chip);
  1187. if (drv_data->ssp_type == CE4100_SSP)
  1188. return 0;
  1189. return setup_cs(spi, chip, chip_info);
  1190. }
  1191. static void cleanup(struct spi_device *spi)
  1192. {
  1193. struct chip_data *chip = spi_get_ctldata(spi);
  1194. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1195. if (!chip)
  1196. return;
  1197. if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
  1198. gpio_is_valid(chip->gpio_cs))
  1199. gpio_free(chip->gpio_cs);
  1200. kfree(chip);
  1201. }
  1202. #ifdef CONFIG_PCI
  1203. #ifdef CONFIG_ACPI
  1204. static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
  1205. { "INT33C0", LPSS_LPT_SSP },
  1206. { "INT33C1", LPSS_LPT_SSP },
  1207. { "INT3430", LPSS_LPT_SSP },
  1208. { "INT3431", LPSS_LPT_SSP },
  1209. { "80860F0E", LPSS_BYT_SSP },
  1210. { "8086228E", LPSS_BSW_SSP },
  1211. { },
  1212. };
  1213. MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
  1214. static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
  1215. {
  1216. unsigned int devid;
  1217. int port_id = -1;
  1218. if (adev && adev->pnp.unique_id &&
  1219. !kstrtouint(adev->pnp.unique_id, 0, &devid))
  1220. port_id = devid;
  1221. return port_id;
  1222. }
  1223. #else /* !CONFIG_ACPI */
  1224. static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
  1225. {
  1226. return -1;
  1227. }
  1228. #endif
  1229. /*
  1230. * PCI IDs of compound devices that integrate both host controller and private
  1231. * integrated DMA engine. Please note these are not used in module
  1232. * autoloading and probing in this module but matching the LPSS SSP type.
  1233. */
  1234. static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
  1235. /* SPT-LP */
  1236. { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
  1237. { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
  1238. /* SPT-H */
  1239. { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
  1240. { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
  1241. /* KBL-H */
  1242. { PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
  1243. { PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
  1244. /* BXT A-Step */
  1245. { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
  1246. { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
  1247. { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
  1248. /* BXT B-Step */
  1249. { PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
  1250. { PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
  1251. { PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
  1252. /* GLK */
  1253. { PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
  1254. { PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
  1255. { PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
  1256. /* APL */
  1257. { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
  1258. { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
  1259. { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
  1260. { },
  1261. };
  1262. static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
  1263. {
  1264. struct device *dev = param;
  1265. if (dev != chan->device->dev->parent)
  1266. return false;
  1267. return true;
  1268. }
  1269. static struct pxa2xx_spi_master *
  1270. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1271. {
  1272. struct pxa2xx_spi_master *pdata;
  1273. struct acpi_device *adev;
  1274. struct ssp_device *ssp;
  1275. struct resource *res;
  1276. const struct acpi_device_id *adev_id = NULL;
  1277. const struct pci_device_id *pcidev_id = NULL;
  1278. int type;
  1279. adev = ACPI_COMPANION(&pdev->dev);
  1280. if (dev_is_pci(pdev->dev.parent))
  1281. pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match,
  1282. to_pci_dev(pdev->dev.parent));
  1283. else if (adev)
  1284. adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
  1285. &pdev->dev);
  1286. else
  1287. return NULL;
  1288. if (adev_id)
  1289. type = (int)adev_id->driver_data;
  1290. else if (pcidev_id)
  1291. type = (int)pcidev_id->driver_data;
  1292. else
  1293. return NULL;
  1294. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1295. if (!pdata)
  1296. return NULL;
  1297. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1298. if (!res)
  1299. return NULL;
  1300. ssp = &pdata->ssp;
  1301. ssp->phys_base = res->start;
  1302. ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
  1303. if (IS_ERR(ssp->mmio_base))
  1304. return NULL;
  1305. if (pcidev_id) {
  1306. pdata->tx_param = pdev->dev.parent;
  1307. pdata->rx_param = pdev->dev.parent;
  1308. pdata->dma_filter = pxa2xx_spi_idma_filter;
  1309. }
  1310. ssp->clk = devm_clk_get(&pdev->dev, NULL);
  1311. ssp->irq = platform_get_irq(pdev, 0);
  1312. ssp->type = type;
  1313. ssp->pdev = pdev;
  1314. ssp->port_id = pxa2xx_spi_get_port_id(adev);
  1315. pdata->num_chipselect = 1;
  1316. pdata->enable_dma = true;
  1317. return pdata;
  1318. }
  1319. #else /* !CONFIG_PCI */
  1320. static inline struct pxa2xx_spi_master *
  1321. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1322. {
  1323. return NULL;
  1324. }
  1325. #endif
  1326. static int pxa2xx_spi_fw_translate_cs(struct spi_master *master, unsigned cs)
  1327. {
  1328. struct driver_data *drv_data = spi_master_get_devdata(master);
  1329. if (has_acpi_companion(&drv_data->pdev->dev)) {
  1330. switch (drv_data->ssp_type) {
  1331. /*
  1332. * For Atoms the ACPI DeviceSelection used by the Windows
  1333. * driver starts from 1 instead of 0 so translate it here
  1334. * to match what Linux expects.
  1335. */
  1336. case LPSS_BYT_SSP:
  1337. case LPSS_BSW_SSP:
  1338. return cs - 1;
  1339. default:
  1340. break;
  1341. }
  1342. }
  1343. return cs;
  1344. }
  1345. static int pxa2xx_spi_probe(struct platform_device *pdev)
  1346. {
  1347. struct device *dev = &pdev->dev;
  1348. struct pxa2xx_spi_master *platform_info;
  1349. struct spi_master *master;
  1350. struct driver_data *drv_data;
  1351. struct ssp_device *ssp;
  1352. const struct lpss_config *config;
  1353. int status, count;
  1354. u32 tmp;
  1355. platform_info = dev_get_platdata(dev);
  1356. if (!platform_info) {
  1357. platform_info = pxa2xx_spi_init_pdata(pdev);
  1358. if (!platform_info) {
  1359. dev_err(&pdev->dev, "missing platform data\n");
  1360. return -ENODEV;
  1361. }
  1362. }
  1363. ssp = pxa_ssp_request(pdev->id, pdev->name);
  1364. if (!ssp)
  1365. ssp = &platform_info->ssp;
  1366. if (!ssp->mmio_base) {
  1367. dev_err(&pdev->dev, "failed to get ssp\n");
  1368. return -ENODEV;
  1369. }
  1370. master = spi_alloc_master(dev, sizeof(struct driver_data));
  1371. if (!master) {
  1372. dev_err(&pdev->dev, "cannot alloc spi_master\n");
  1373. pxa_ssp_free(ssp);
  1374. return -ENOMEM;
  1375. }
  1376. drv_data = spi_master_get_devdata(master);
  1377. drv_data->master = master;
  1378. drv_data->master_info = platform_info;
  1379. drv_data->pdev = pdev;
  1380. drv_data->ssp = ssp;
  1381. master->dev.of_node = pdev->dev.of_node;
  1382. /* the spi->mode bits understood by this driver: */
  1383. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  1384. master->bus_num = ssp->port_id;
  1385. master->dma_alignment = DMA_ALIGNMENT;
  1386. master->cleanup = cleanup;
  1387. master->setup = setup;
  1388. master->transfer_one_message = pxa2xx_spi_transfer_one_message;
  1389. master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
  1390. master->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
  1391. master->auto_runtime_pm = true;
  1392. master->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX;
  1393. drv_data->ssp_type = ssp->type;
  1394. drv_data->ioaddr = ssp->mmio_base;
  1395. drv_data->ssdr_physical = ssp->phys_base + SSDR;
  1396. if (pxa25x_ssp_comp(drv_data)) {
  1397. switch (drv_data->ssp_type) {
  1398. case QUARK_X1000_SSP:
  1399. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1400. break;
  1401. default:
  1402. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  1403. break;
  1404. }
  1405. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
  1406. drv_data->dma_cr1 = 0;
  1407. drv_data->clear_sr = SSSR_ROR;
  1408. drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1409. } else {
  1410. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1411. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
  1412. drv_data->dma_cr1 = DEFAULT_DMA_CR1;
  1413. drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
  1414. drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1415. }
  1416. status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
  1417. drv_data);
  1418. if (status < 0) {
  1419. dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
  1420. goto out_error_master_alloc;
  1421. }
  1422. /* Setup DMA if requested */
  1423. if (platform_info->enable_dma) {
  1424. status = pxa2xx_spi_dma_setup(drv_data);
  1425. if (status) {
  1426. dev_dbg(dev, "no DMA channels available, using PIO\n");
  1427. platform_info->enable_dma = false;
  1428. } else {
  1429. master->can_dma = pxa2xx_spi_can_dma;
  1430. }
  1431. }
  1432. /* Enable SOC clock */
  1433. clk_prepare_enable(ssp->clk);
  1434. master->max_speed_hz = clk_get_rate(ssp->clk);
  1435. /* Load default SSP configuration */
  1436. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1437. switch (drv_data->ssp_type) {
  1438. case QUARK_X1000_SSP:
  1439. tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
  1440. QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
  1441. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1442. /* using the Motorola SPI protocol and use 8 bit frame */
  1443. tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
  1444. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1445. break;
  1446. case CE4100_SSP:
  1447. tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
  1448. CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
  1449. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1450. tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
  1451. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1452. break;
  1453. default:
  1454. tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
  1455. SSCR1_TxTresh(TX_THRESH_DFLT);
  1456. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1457. tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
  1458. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1459. break;
  1460. }
  1461. if (!pxa25x_ssp_comp(drv_data))
  1462. pxa2xx_spi_write(drv_data, SSTO, 0);
  1463. if (!is_quark_x1000_ssp(drv_data))
  1464. pxa2xx_spi_write(drv_data, SSPSP, 0);
  1465. if (is_lpss_ssp(drv_data)) {
  1466. lpss_ssp_setup(drv_data);
  1467. config = lpss_get_config(drv_data);
  1468. if (config->reg_capabilities >= 0) {
  1469. tmp = __lpss_ssp_read_priv(drv_data,
  1470. config->reg_capabilities);
  1471. tmp &= LPSS_CAPS_CS_EN_MASK;
  1472. tmp >>= LPSS_CAPS_CS_EN_SHIFT;
  1473. platform_info->num_chipselect = ffz(tmp);
  1474. } else if (config->cs_num) {
  1475. platform_info->num_chipselect = config->cs_num;
  1476. }
  1477. }
  1478. master->num_chipselect = platform_info->num_chipselect;
  1479. count = gpiod_count(&pdev->dev, "cs");
  1480. if (count > 0) {
  1481. int i;
  1482. master->num_chipselect = max_t(int, count,
  1483. master->num_chipselect);
  1484. drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
  1485. master->num_chipselect, sizeof(struct gpio_desc *),
  1486. GFP_KERNEL);
  1487. if (!drv_data->cs_gpiods) {
  1488. status = -ENOMEM;
  1489. goto out_error_clock_enabled;
  1490. }
  1491. for (i = 0; i < master->num_chipselect; i++) {
  1492. struct gpio_desc *gpiod;
  1493. gpiod = devm_gpiod_get_index(dev, "cs", i,
  1494. GPIOD_OUT_HIGH);
  1495. if (IS_ERR(gpiod)) {
  1496. /* Means use native chip select */
  1497. if (PTR_ERR(gpiod) == -ENOENT)
  1498. continue;
  1499. status = (int)PTR_ERR(gpiod);
  1500. goto out_error_clock_enabled;
  1501. } else {
  1502. drv_data->cs_gpiods[i] = gpiod;
  1503. }
  1504. }
  1505. }
  1506. tasklet_init(&drv_data->pump_transfers, pump_transfers,
  1507. (unsigned long)drv_data);
  1508. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  1509. pm_runtime_use_autosuspend(&pdev->dev);
  1510. pm_runtime_set_active(&pdev->dev);
  1511. pm_runtime_enable(&pdev->dev);
  1512. /* Register with the SPI framework */
  1513. platform_set_drvdata(pdev, drv_data);
  1514. status = devm_spi_register_master(&pdev->dev, master);
  1515. if (status != 0) {
  1516. dev_err(&pdev->dev, "problem registering spi master\n");
  1517. goto out_error_clock_enabled;
  1518. }
  1519. return status;
  1520. out_error_clock_enabled:
  1521. clk_disable_unprepare(ssp->clk);
  1522. pxa2xx_spi_dma_release(drv_data);
  1523. free_irq(ssp->irq, drv_data);
  1524. out_error_master_alloc:
  1525. spi_master_put(master);
  1526. pxa_ssp_free(ssp);
  1527. return status;
  1528. }
  1529. static int pxa2xx_spi_remove(struct platform_device *pdev)
  1530. {
  1531. struct driver_data *drv_data = platform_get_drvdata(pdev);
  1532. struct ssp_device *ssp;
  1533. if (!drv_data)
  1534. return 0;
  1535. ssp = drv_data->ssp;
  1536. pm_runtime_get_sync(&pdev->dev);
  1537. /* Disable the SSP at the peripheral and SOC level */
  1538. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1539. clk_disable_unprepare(ssp->clk);
  1540. /* Release DMA */
  1541. if (drv_data->master_info->enable_dma)
  1542. pxa2xx_spi_dma_release(drv_data);
  1543. pm_runtime_put_noidle(&pdev->dev);
  1544. pm_runtime_disable(&pdev->dev);
  1545. /* Release IRQ */
  1546. free_irq(ssp->irq, drv_data);
  1547. /* Release SSP */
  1548. pxa_ssp_free(ssp);
  1549. return 0;
  1550. }
  1551. static void pxa2xx_spi_shutdown(struct platform_device *pdev)
  1552. {
  1553. int status = 0;
  1554. if ((status = pxa2xx_spi_remove(pdev)) != 0)
  1555. dev_err(&pdev->dev, "shutdown failed with %d\n", status);
  1556. }
  1557. #ifdef CONFIG_PM_SLEEP
  1558. static int pxa2xx_spi_suspend(struct device *dev)
  1559. {
  1560. struct driver_data *drv_data = dev_get_drvdata(dev);
  1561. struct ssp_device *ssp = drv_data->ssp;
  1562. int status;
  1563. status = spi_master_suspend(drv_data->master);
  1564. if (status != 0)
  1565. return status;
  1566. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1567. if (!pm_runtime_suspended(dev))
  1568. clk_disable_unprepare(ssp->clk);
  1569. return 0;
  1570. }
  1571. static int pxa2xx_spi_resume(struct device *dev)
  1572. {
  1573. struct driver_data *drv_data = dev_get_drvdata(dev);
  1574. struct ssp_device *ssp = drv_data->ssp;
  1575. int status;
  1576. /* Enable the SSP clock */
  1577. if (!pm_runtime_suspended(dev))
  1578. clk_prepare_enable(ssp->clk);
  1579. /* Restore LPSS private register bits */
  1580. if (is_lpss_ssp(drv_data))
  1581. lpss_ssp_setup(drv_data);
  1582. /* Start the queue running */
  1583. status = spi_master_resume(drv_data->master);
  1584. if (status != 0) {
  1585. dev_err(dev, "problem starting queue (%d)\n", status);
  1586. return status;
  1587. }
  1588. return 0;
  1589. }
  1590. #endif
  1591. #ifdef CONFIG_PM
  1592. static int pxa2xx_spi_runtime_suspend(struct device *dev)
  1593. {
  1594. struct driver_data *drv_data = dev_get_drvdata(dev);
  1595. clk_disable_unprepare(drv_data->ssp->clk);
  1596. return 0;
  1597. }
  1598. static int pxa2xx_spi_runtime_resume(struct device *dev)
  1599. {
  1600. struct driver_data *drv_data = dev_get_drvdata(dev);
  1601. clk_prepare_enable(drv_data->ssp->clk);
  1602. return 0;
  1603. }
  1604. #endif
  1605. static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
  1606. SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
  1607. SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
  1608. pxa2xx_spi_runtime_resume, NULL)
  1609. };
  1610. static struct platform_driver driver = {
  1611. .driver = {
  1612. .name = "pxa2xx-spi",
  1613. .pm = &pxa2xx_spi_pm_ops,
  1614. .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
  1615. },
  1616. .probe = pxa2xx_spi_probe,
  1617. .remove = pxa2xx_spi_remove,
  1618. .shutdown = pxa2xx_spi_shutdown,
  1619. };
  1620. static int __init pxa2xx_spi_init(void)
  1621. {
  1622. return platform_driver_register(&driver);
  1623. }
  1624. subsys_initcall(pxa2xx_spi_init);
  1625. static void __exit pxa2xx_spi_exit(void)
  1626. {
  1627. platform_driver_unregister(&driver);
  1628. }
  1629. module_exit(pxa2xx_spi_exit);