pci.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /*
  2. * Sonics Silicon Backplane PCI-Hostbus related functions.
  3. *
  4. * Copyright (C) 2005-2006 Michael Buesch <m@bues.ch>
  5. * Copyright (C) 2005 Martin Langer <martin-langer@gmx.de>
  6. * Copyright (C) 2005 Stefano Brivio <st3@riseup.net>
  7. * Copyright (C) 2005 Danny van Dyk <kugelfang@gentoo.org>
  8. * Copyright (C) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
  9. *
  10. * Derived from the Broadcom 4400 device driver.
  11. * Copyright (C) 2002 David S. Miller (davem@redhat.com)
  12. * Fixed by Pekka Pietikainen (pp@ee.oulu.fi)
  13. * Copyright (C) 2006 Broadcom Corporation.
  14. *
  15. * Licensed under the GNU/GPL. See COPYING for details.
  16. */
  17. #include <linux/ssb/ssb.h>
  18. #include <linux/ssb/ssb_regs.h>
  19. #include <linux/slab.h>
  20. #include <linux/pci.h>
  21. #include <linux/delay.h>
  22. #include "ssb_private.h"
  23. /* Define the following to 1 to enable a printk on each coreswitch. */
  24. #define SSB_VERBOSE_PCICORESWITCH_DEBUG 0
  25. /* Lowlevel coreswitching */
  26. int ssb_pci_switch_coreidx(struct ssb_bus *bus, u8 coreidx)
  27. {
  28. int err;
  29. int attempts = 0;
  30. u32 cur_core;
  31. while (1) {
  32. err = pci_write_config_dword(bus->host_pci, SSB_BAR0_WIN,
  33. (coreidx * SSB_CORE_SIZE)
  34. + SSB_ENUM_BASE);
  35. if (err)
  36. goto error;
  37. err = pci_read_config_dword(bus->host_pci, SSB_BAR0_WIN,
  38. &cur_core);
  39. if (err)
  40. goto error;
  41. cur_core = (cur_core - SSB_ENUM_BASE)
  42. / SSB_CORE_SIZE;
  43. if (cur_core == coreidx)
  44. break;
  45. if (attempts++ > SSB_BAR0_MAX_RETRIES)
  46. goto error;
  47. udelay(10);
  48. }
  49. return 0;
  50. error:
  51. ssb_printk(KERN_ERR PFX "Failed to switch to core %u\n", coreidx);
  52. return -ENODEV;
  53. }
  54. int ssb_pci_switch_core(struct ssb_bus *bus,
  55. struct ssb_device *dev)
  56. {
  57. int err;
  58. unsigned long flags;
  59. #if SSB_VERBOSE_PCICORESWITCH_DEBUG
  60. ssb_printk(KERN_INFO PFX
  61. "Switching to %s core, index %d\n",
  62. ssb_core_name(dev->id.coreid),
  63. dev->core_index);
  64. #endif
  65. spin_lock_irqsave(&bus->bar_lock, flags);
  66. err = ssb_pci_switch_coreidx(bus, dev->core_index);
  67. if (!err)
  68. bus->mapped_device = dev;
  69. spin_unlock_irqrestore(&bus->bar_lock, flags);
  70. return err;
  71. }
  72. /* Enable/disable the on board crystal oscillator and/or PLL. */
  73. int ssb_pci_xtal(struct ssb_bus *bus, u32 what, int turn_on)
  74. {
  75. int err;
  76. u32 in, out, outenable;
  77. u16 pci_status;
  78. if (bus->bustype != SSB_BUSTYPE_PCI)
  79. return 0;
  80. err = pci_read_config_dword(bus->host_pci, SSB_GPIO_IN, &in);
  81. if (err)
  82. goto err_pci;
  83. err = pci_read_config_dword(bus->host_pci, SSB_GPIO_OUT, &out);
  84. if (err)
  85. goto err_pci;
  86. err = pci_read_config_dword(bus->host_pci, SSB_GPIO_OUT_ENABLE, &outenable);
  87. if (err)
  88. goto err_pci;
  89. outenable |= what;
  90. if (turn_on) {
  91. /* Avoid glitching the clock if GPRS is already using it.
  92. * We can't actually read the state of the PLLPD so we infer it
  93. * by the value of XTAL_PU which *is* readable via gpioin.
  94. */
  95. if (!(in & SSB_GPIO_XTAL)) {
  96. if (what & SSB_GPIO_XTAL) {
  97. /* Turn the crystal on */
  98. out |= SSB_GPIO_XTAL;
  99. if (what & SSB_GPIO_PLL)
  100. out |= SSB_GPIO_PLL;
  101. err = pci_write_config_dword(bus->host_pci, SSB_GPIO_OUT, out);
  102. if (err)
  103. goto err_pci;
  104. err = pci_write_config_dword(bus->host_pci, SSB_GPIO_OUT_ENABLE,
  105. outenable);
  106. if (err)
  107. goto err_pci;
  108. msleep(1);
  109. }
  110. if (what & SSB_GPIO_PLL) {
  111. /* Turn the PLL on */
  112. out &= ~SSB_GPIO_PLL;
  113. err = pci_write_config_dword(bus->host_pci, SSB_GPIO_OUT, out);
  114. if (err)
  115. goto err_pci;
  116. msleep(5);
  117. }
  118. }
  119. err = pci_read_config_word(bus->host_pci, PCI_STATUS, &pci_status);
  120. if (err)
  121. goto err_pci;
  122. pci_status &= ~PCI_STATUS_SIG_TARGET_ABORT;
  123. err = pci_write_config_word(bus->host_pci, PCI_STATUS, pci_status);
  124. if (err)
  125. goto err_pci;
  126. } else {
  127. if (what & SSB_GPIO_XTAL) {
  128. /* Turn the crystal off */
  129. out &= ~SSB_GPIO_XTAL;
  130. }
  131. if (what & SSB_GPIO_PLL) {
  132. /* Turn the PLL off */
  133. out |= SSB_GPIO_PLL;
  134. }
  135. err = pci_write_config_dword(bus->host_pci, SSB_GPIO_OUT, out);
  136. if (err)
  137. goto err_pci;
  138. err = pci_write_config_dword(bus->host_pci, SSB_GPIO_OUT_ENABLE, outenable);
  139. if (err)
  140. goto err_pci;
  141. }
  142. out:
  143. return err;
  144. err_pci:
  145. printk(KERN_ERR PFX "Error: ssb_pci_xtal() could not access PCI config space!\n");
  146. err = -EBUSY;
  147. goto out;
  148. }
  149. /* Get the word-offset for a SSB_SPROM_XXX define. */
  150. #define SPOFF(offset) ((offset) / sizeof(u16))
  151. /* Helper to extract some _offset, which is one of the SSB_SPROM_XXX defines. */
  152. #define SPEX16(_outvar, _offset, _mask, _shift) \
  153. out->_outvar = ((in[SPOFF(_offset)] & (_mask)) >> (_shift))
  154. #define SPEX32(_outvar, _offset, _mask, _shift) \
  155. out->_outvar = ((((u32)in[SPOFF((_offset)+2)] << 16 | \
  156. in[SPOFF(_offset)]) & (_mask)) >> (_shift))
  157. #define SPEX(_outvar, _offset, _mask, _shift) \
  158. SPEX16(_outvar, _offset, _mask, _shift)
  159. static inline u8 ssb_crc8(u8 crc, u8 data)
  160. {
  161. /* Polynomial: x^8 + x^7 + x^6 + x^4 + x^2 + 1 */
  162. static const u8 t[] = {
  163. 0x00, 0xF7, 0xB9, 0x4E, 0x25, 0xD2, 0x9C, 0x6B,
  164. 0x4A, 0xBD, 0xF3, 0x04, 0x6F, 0x98, 0xD6, 0x21,
  165. 0x94, 0x63, 0x2D, 0xDA, 0xB1, 0x46, 0x08, 0xFF,
  166. 0xDE, 0x29, 0x67, 0x90, 0xFB, 0x0C, 0x42, 0xB5,
  167. 0x7F, 0x88, 0xC6, 0x31, 0x5A, 0xAD, 0xE3, 0x14,
  168. 0x35, 0xC2, 0x8C, 0x7B, 0x10, 0xE7, 0xA9, 0x5E,
  169. 0xEB, 0x1C, 0x52, 0xA5, 0xCE, 0x39, 0x77, 0x80,
  170. 0xA1, 0x56, 0x18, 0xEF, 0x84, 0x73, 0x3D, 0xCA,
  171. 0xFE, 0x09, 0x47, 0xB0, 0xDB, 0x2C, 0x62, 0x95,
  172. 0xB4, 0x43, 0x0D, 0xFA, 0x91, 0x66, 0x28, 0xDF,
  173. 0x6A, 0x9D, 0xD3, 0x24, 0x4F, 0xB8, 0xF6, 0x01,
  174. 0x20, 0xD7, 0x99, 0x6E, 0x05, 0xF2, 0xBC, 0x4B,
  175. 0x81, 0x76, 0x38, 0xCF, 0xA4, 0x53, 0x1D, 0xEA,
  176. 0xCB, 0x3C, 0x72, 0x85, 0xEE, 0x19, 0x57, 0xA0,
  177. 0x15, 0xE2, 0xAC, 0x5B, 0x30, 0xC7, 0x89, 0x7E,
  178. 0x5F, 0xA8, 0xE6, 0x11, 0x7A, 0x8D, 0xC3, 0x34,
  179. 0xAB, 0x5C, 0x12, 0xE5, 0x8E, 0x79, 0x37, 0xC0,
  180. 0xE1, 0x16, 0x58, 0xAF, 0xC4, 0x33, 0x7D, 0x8A,
  181. 0x3F, 0xC8, 0x86, 0x71, 0x1A, 0xED, 0xA3, 0x54,
  182. 0x75, 0x82, 0xCC, 0x3B, 0x50, 0xA7, 0xE9, 0x1E,
  183. 0xD4, 0x23, 0x6D, 0x9A, 0xF1, 0x06, 0x48, 0xBF,
  184. 0x9E, 0x69, 0x27, 0xD0, 0xBB, 0x4C, 0x02, 0xF5,
  185. 0x40, 0xB7, 0xF9, 0x0E, 0x65, 0x92, 0xDC, 0x2B,
  186. 0x0A, 0xFD, 0xB3, 0x44, 0x2F, 0xD8, 0x96, 0x61,
  187. 0x55, 0xA2, 0xEC, 0x1B, 0x70, 0x87, 0xC9, 0x3E,
  188. 0x1F, 0xE8, 0xA6, 0x51, 0x3A, 0xCD, 0x83, 0x74,
  189. 0xC1, 0x36, 0x78, 0x8F, 0xE4, 0x13, 0x5D, 0xAA,
  190. 0x8B, 0x7C, 0x32, 0xC5, 0xAE, 0x59, 0x17, 0xE0,
  191. 0x2A, 0xDD, 0x93, 0x64, 0x0F, 0xF8, 0xB6, 0x41,
  192. 0x60, 0x97, 0xD9, 0x2E, 0x45, 0xB2, 0xFC, 0x0B,
  193. 0xBE, 0x49, 0x07, 0xF0, 0x9B, 0x6C, 0x22, 0xD5,
  194. 0xF4, 0x03, 0x4D, 0xBA, 0xD1, 0x26, 0x68, 0x9F,
  195. };
  196. return t[crc ^ data];
  197. }
  198. static u8 ssb_sprom_crc(const u16 *sprom, u16 size)
  199. {
  200. int word;
  201. u8 crc = 0xFF;
  202. for (word = 0; word < size - 1; word++) {
  203. crc = ssb_crc8(crc, sprom[word] & 0x00FF);
  204. crc = ssb_crc8(crc, (sprom[word] & 0xFF00) >> 8);
  205. }
  206. crc = ssb_crc8(crc, sprom[size - 1] & 0x00FF);
  207. crc ^= 0xFF;
  208. return crc;
  209. }
  210. static int sprom_check_crc(const u16 *sprom, size_t size)
  211. {
  212. u8 crc;
  213. u8 expected_crc;
  214. u16 tmp;
  215. crc = ssb_sprom_crc(sprom, size);
  216. tmp = sprom[size - 1] & SSB_SPROM_REVISION_CRC;
  217. expected_crc = tmp >> SSB_SPROM_REVISION_CRC_SHIFT;
  218. if (crc != expected_crc)
  219. return -EPROTO;
  220. return 0;
  221. }
  222. static int sprom_do_read(struct ssb_bus *bus, u16 *sprom)
  223. {
  224. int i;
  225. for (i = 0; i < bus->sprom_size; i++)
  226. sprom[i] = ioread16(bus->mmio + bus->sprom_offset + (i * 2));
  227. return 0;
  228. }
  229. static int sprom_do_write(struct ssb_bus *bus, const u16 *sprom)
  230. {
  231. struct pci_dev *pdev = bus->host_pci;
  232. int i, err;
  233. u32 spromctl;
  234. u16 size = bus->sprom_size;
  235. ssb_printk(KERN_NOTICE PFX "Writing SPROM. Do NOT turn off the power! Please stand by...\n");
  236. err = pci_read_config_dword(pdev, SSB_SPROMCTL, &spromctl);
  237. if (err)
  238. goto err_ctlreg;
  239. spromctl |= SSB_SPROMCTL_WE;
  240. err = pci_write_config_dword(pdev, SSB_SPROMCTL, spromctl);
  241. if (err)
  242. goto err_ctlreg;
  243. ssb_printk(KERN_NOTICE PFX "[ 0%%");
  244. msleep(500);
  245. for (i = 0; i < size; i++) {
  246. if (i == size / 4)
  247. ssb_printk("25%%");
  248. else if (i == size / 2)
  249. ssb_printk("50%%");
  250. else if (i == (size * 3) / 4)
  251. ssb_printk("75%%");
  252. else if (i % 2)
  253. ssb_printk(".");
  254. writew(sprom[i], bus->mmio + bus->sprom_offset + (i * 2));
  255. mmiowb();
  256. msleep(20);
  257. }
  258. err = pci_read_config_dword(pdev, SSB_SPROMCTL, &spromctl);
  259. if (err)
  260. goto err_ctlreg;
  261. spromctl &= ~SSB_SPROMCTL_WE;
  262. err = pci_write_config_dword(pdev, SSB_SPROMCTL, spromctl);
  263. if (err)
  264. goto err_ctlreg;
  265. msleep(500);
  266. ssb_printk("100%% ]\n");
  267. ssb_printk(KERN_NOTICE PFX "SPROM written.\n");
  268. return 0;
  269. err_ctlreg:
  270. ssb_printk(KERN_ERR PFX "Could not access SPROM control register.\n");
  271. return err;
  272. }
  273. static s8 r123_extract_antgain(u8 sprom_revision, const u16 *in,
  274. u16 mask, u16 shift)
  275. {
  276. u16 v;
  277. u8 gain;
  278. v = in[SPOFF(SSB_SPROM1_AGAIN)];
  279. gain = (v & mask) >> shift;
  280. if (gain == 0xFF)
  281. gain = 2; /* If unset use 2dBm */
  282. if (sprom_revision == 1) {
  283. /* Convert to Q5.2 */
  284. gain <<= 2;
  285. } else {
  286. /* Q5.2 Fractional part is stored in 0xC0 */
  287. gain = ((gain & 0xC0) >> 6) | ((gain & 0x3F) << 2);
  288. }
  289. return (s8)gain;
  290. }
  291. static void sprom_extract_r123(struct ssb_sprom *out, const u16 *in)
  292. {
  293. int i;
  294. u16 v;
  295. u16 loc[3];
  296. if (out->revision == 3) /* rev 3 moved MAC */
  297. loc[0] = SSB_SPROM3_IL0MAC;
  298. else {
  299. loc[0] = SSB_SPROM1_IL0MAC;
  300. loc[1] = SSB_SPROM1_ET0MAC;
  301. loc[2] = SSB_SPROM1_ET1MAC;
  302. }
  303. for (i = 0; i < 3; i++) {
  304. v = in[SPOFF(loc[0]) + i];
  305. *(((__be16 *)out->il0mac) + i) = cpu_to_be16(v);
  306. }
  307. if (out->revision < 3) { /* only rev 1-2 have et0, et1 */
  308. for (i = 0; i < 3; i++) {
  309. v = in[SPOFF(loc[1]) + i];
  310. *(((__be16 *)out->et0mac) + i) = cpu_to_be16(v);
  311. }
  312. for (i = 0; i < 3; i++) {
  313. v = in[SPOFF(loc[2]) + i];
  314. *(((__be16 *)out->et1mac) + i) = cpu_to_be16(v);
  315. }
  316. }
  317. SPEX(et0phyaddr, SSB_SPROM1_ETHPHY, SSB_SPROM1_ETHPHY_ET0A, 0);
  318. SPEX(et1phyaddr, SSB_SPROM1_ETHPHY, SSB_SPROM1_ETHPHY_ET1A,
  319. SSB_SPROM1_ETHPHY_ET1A_SHIFT);
  320. SPEX(et0mdcport, SSB_SPROM1_ETHPHY, SSB_SPROM1_ETHPHY_ET0M, 14);
  321. SPEX(et1mdcport, SSB_SPROM1_ETHPHY, SSB_SPROM1_ETHPHY_ET1M, 15);
  322. SPEX(board_rev, SSB_SPROM1_BINF, SSB_SPROM1_BINF_BREV, 0);
  323. SPEX(country_code, SSB_SPROM1_BINF, SSB_SPROM1_BINF_CCODE,
  324. SSB_SPROM1_BINF_CCODE_SHIFT);
  325. SPEX(ant_available_a, SSB_SPROM1_BINF, SSB_SPROM1_BINF_ANTA,
  326. SSB_SPROM1_BINF_ANTA_SHIFT);
  327. SPEX(ant_available_bg, SSB_SPROM1_BINF, SSB_SPROM1_BINF_ANTBG,
  328. SSB_SPROM1_BINF_ANTBG_SHIFT);
  329. SPEX(pa0b0, SSB_SPROM1_PA0B0, 0xFFFF, 0);
  330. SPEX(pa0b1, SSB_SPROM1_PA0B1, 0xFFFF, 0);
  331. SPEX(pa0b2, SSB_SPROM1_PA0B2, 0xFFFF, 0);
  332. SPEX(pa1b0, SSB_SPROM1_PA1B0, 0xFFFF, 0);
  333. SPEX(pa1b1, SSB_SPROM1_PA1B1, 0xFFFF, 0);
  334. SPEX(pa1b2, SSB_SPROM1_PA1B2, 0xFFFF, 0);
  335. SPEX(gpio0, SSB_SPROM1_GPIOA, SSB_SPROM1_GPIOA_P0, 0);
  336. SPEX(gpio1, SSB_SPROM1_GPIOA, SSB_SPROM1_GPIOA_P1,
  337. SSB_SPROM1_GPIOA_P1_SHIFT);
  338. SPEX(gpio2, SSB_SPROM1_GPIOB, SSB_SPROM1_GPIOB_P2, 0);
  339. SPEX(gpio3, SSB_SPROM1_GPIOB, SSB_SPROM1_GPIOB_P3,
  340. SSB_SPROM1_GPIOB_P3_SHIFT);
  341. SPEX(maxpwr_a, SSB_SPROM1_MAXPWR, SSB_SPROM1_MAXPWR_A,
  342. SSB_SPROM1_MAXPWR_A_SHIFT);
  343. SPEX(maxpwr_bg, SSB_SPROM1_MAXPWR, SSB_SPROM1_MAXPWR_BG, 0);
  344. SPEX(itssi_a, SSB_SPROM1_ITSSI, SSB_SPROM1_ITSSI_A,
  345. SSB_SPROM1_ITSSI_A_SHIFT);
  346. SPEX(itssi_bg, SSB_SPROM1_ITSSI, SSB_SPROM1_ITSSI_BG, 0);
  347. SPEX(boardflags_lo, SSB_SPROM1_BFLLO, 0xFFFF, 0);
  348. if (out->revision >= 2)
  349. SPEX(boardflags_hi, SSB_SPROM2_BFLHI, 0xFFFF, 0);
  350. /* Extract the antenna gain values. */
  351. out->antenna_gain.a0 = r123_extract_antgain(out->revision, in,
  352. SSB_SPROM1_AGAIN_BG,
  353. SSB_SPROM1_AGAIN_BG_SHIFT);
  354. out->antenna_gain.a1 = r123_extract_antgain(out->revision, in,
  355. SSB_SPROM1_AGAIN_A,
  356. SSB_SPROM1_AGAIN_A_SHIFT);
  357. }
  358. /* Revs 4 5 and 8 have partially shared layout */
  359. static void sprom_extract_r458(struct ssb_sprom *out, const u16 *in)
  360. {
  361. SPEX(txpid2g[0], SSB_SPROM4_TXPID2G01,
  362. SSB_SPROM4_TXPID2G0, SSB_SPROM4_TXPID2G0_SHIFT);
  363. SPEX(txpid2g[1], SSB_SPROM4_TXPID2G01,
  364. SSB_SPROM4_TXPID2G1, SSB_SPROM4_TXPID2G1_SHIFT);
  365. SPEX(txpid2g[2], SSB_SPROM4_TXPID2G23,
  366. SSB_SPROM4_TXPID2G2, SSB_SPROM4_TXPID2G2_SHIFT);
  367. SPEX(txpid2g[3], SSB_SPROM4_TXPID2G23,
  368. SSB_SPROM4_TXPID2G3, SSB_SPROM4_TXPID2G3_SHIFT);
  369. SPEX(txpid5gl[0], SSB_SPROM4_TXPID5GL01,
  370. SSB_SPROM4_TXPID5GL0, SSB_SPROM4_TXPID5GL0_SHIFT);
  371. SPEX(txpid5gl[1], SSB_SPROM4_TXPID5GL01,
  372. SSB_SPROM4_TXPID5GL1, SSB_SPROM4_TXPID5GL1_SHIFT);
  373. SPEX(txpid5gl[2], SSB_SPROM4_TXPID5GL23,
  374. SSB_SPROM4_TXPID5GL2, SSB_SPROM4_TXPID5GL2_SHIFT);
  375. SPEX(txpid5gl[3], SSB_SPROM4_TXPID5GL23,
  376. SSB_SPROM4_TXPID5GL3, SSB_SPROM4_TXPID5GL3_SHIFT);
  377. SPEX(txpid5g[0], SSB_SPROM4_TXPID5G01,
  378. SSB_SPROM4_TXPID5G0, SSB_SPROM4_TXPID5G0_SHIFT);
  379. SPEX(txpid5g[1], SSB_SPROM4_TXPID5G01,
  380. SSB_SPROM4_TXPID5G1, SSB_SPROM4_TXPID5G1_SHIFT);
  381. SPEX(txpid5g[2], SSB_SPROM4_TXPID5G23,
  382. SSB_SPROM4_TXPID5G2, SSB_SPROM4_TXPID5G2_SHIFT);
  383. SPEX(txpid5g[3], SSB_SPROM4_TXPID5G23,
  384. SSB_SPROM4_TXPID5G3, SSB_SPROM4_TXPID5G3_SHIFT);
  385. SPEX(txpid5gh[0], SSB_SPROM4_TXPID5GH01,
  386. SSB_SPROM4_TXPID5GH0, SSB_SPROM4_TXPID5GH0_SHIFT);
  387. SPEX(txpid5gh[1], SSB_SPROM4_TXPID5GH01,
  388. SSB_SPROM4_TXPID5GH1, SSB_SPROM4_TXPID5GH1_SHIFT);
  389. SPEX(txpid5gh[2], SSB_SPROM4_TXPID5GH23,
  390. SSB_SPROM4_TXPID5GH2, SSB_SPROM4_TXPID5GH2_SHIFT);
  391. SPEX(txpid5gh[3], SSB_SPROM4_TXPID5GH23,
  392. SSB_SPROM4_TXPID5GH3, SSB_SPROM4_TXPID5GH3_SHIFT);
  393. }
  394. static void sprom_extract_r45(struct ssb_sprom *out, const u16 *in)
  395. {
  396. int i;
  397. u16 v;
  398. u16 il0mac_offset;
  399. if (out->revision == 4)
  400. il0mac_offset = SSB_SPROM4_IL0MAC;
  401. else
  402. il0mac_offset = SSB_SPROM5_IL0MAC;
  403. /* extract the MAC address */
  404. for (i = 0; i < 3; i++) {
  405. v = in[SPOFF(il0mac_offset) + i];
  406. *(((__be16 *)out->il0mac) + i) = cpu_to_be16(v);
  407. }
  408. SPEX(et0phyaddr, SSB_SPROM4_ETHPHY, SSB_SPROM4_ETHPHY_ET0A, 0);
  409. SPEX(et1phyaddr, SSB_SPROM4_ETHPHY, SSB_SPROM4_ETHPHY_ET1A,
  410. SSB_SPROM4_ETHPHY_ET1A_SHIFT);
  411. if (out->revision == 4) {
  412. SPEX(country_code, SSB_SPROM4_CCODE, 0xFFFF, 0);
  413. SPEX(boardflags_lo, SSB_SPROM4_BFLLO, 0xFFFF, 0);
  414. SPEX(boardflags_hi, SSB_SPROM4_BFLHI, 0xFFFF, 0);
  415. SPEX(boardflags2_lo, SSB_SPROM4_BFL2LO, 0xFFFF, 0);
  416. SPEX(boardflags2_hi, SSB_SPROM4_BFL2HI, 0xFFFF, 0);
  417. } else {
  418. SPEX(country_code, SSB_SPROM5_CCODE, 0xFFFF, 0);
  419. SPEX(boardflags_lo, SSB_SPROM5_BFLLO, 0xFFFF, 0);
  420. SPEX(boardflags_hi, SSB_SPROM5_BFLHI, 0xFFFF, 0);
  421. SPEX(boardflags2_lo, SSB_SPROM5_BFL2LO, 0xFFFF, 0);
  422. SPEX(boardflags2_hi, SSB_SPROM5_BFL2HI, 0xFFFF, 0);
  423. }
  424. SPEX(ant_available_a, SSB_SPROM4_ANTAVAIL, SSB_SPROM4_ANTAVAIL_A,
  425. SSB_SPROM4_ANTAVAIL_A_SHIFT);
  426. SPEX(ant_available_bg, SSB_SPROM4_ANTAVAIL, SSB_SPROM4_ANTAVAIL_BG,
  427. SSB_SPROM4_ANTAVAIL_BG_SHIFT);
  428. SPEX(maxpwr_bg, SSB_SPROM4_MAXP_BG, SSB_SPROM4_MAXP_BG_MASK, 0);
  429. SPEX(itssi_bg, SSB_SPROM4_MAXP_BG, SSB_SPROM4_ITSSI_BG,
  430. SSB_SPROM4_ITSSI_BG_SHIFT);
  431. SPEX(maxpwr_a, SSB_SPROM4_MAXP_A, SSB_SPROM4_MAXP_A_MASK, 0);
  432. SPEX(itssi_a, SSB_SPROM4_MAXP_A, SSB_SPROM4_ITSSI_A,
  433. SSB_SPROM4_ITSSI_A_SHIFT);
  434. if (out->revision == 4) {
  435. SPEX(gpio0, SSB_SPROM4_GPIOA, SSB_SPROM4_GPIOA_P0, 0);
  436. SPEX(gpio1, SSB_SPROM4_GPIOA, SSB_SPROM4_GPIOA_P1,
  437. SSB_SPROM4_GPIOA_P1_SHIFT);
  438. SPEX(gpio2, SSB_SPROM4_GPIOB, SSB_SPROM4_GPIOB_P2, 0);
  439. SPEX(gpio3, SSB_SPROM4_GPIOB, SSB_SPROM4_GPIOB_P3,
  440. SSB_SPROM4_GPIOB_P3_SHIFT);
  441. } else {
  442. SPEX(gpio0, SSB_SPROM5_GPIOA, SSB_SPROM5_GPIOA_P0, 0);
  443. SPEX(gpio1, SSB_SPROM5_GPIOA, SSB_SPROM5_GPIOA_P1,
  444. SSB_SPROM5_GPIOA_P1_SHIFT);
  445. SPEX(gpio2, SSB_SPROM5_GPIOB, SSB_SPROM5_GPIOB_P2, 0);
  446. SPEX(gpio3, SSB_SPROM5_GPIOB, SSB_SPROM5_GPIOB_P3,
  447. SSB_SPROM5_GPIOB_P3_SHIFT);
  448. }
  449. /* Extract the antenna gain values. */
  450. SPEX(antenna_gain.a0, SSB_SPROM4_AGAIN01,
  451. SSB_SPROM4_AGAIN0, SSB_SPROM4_AGAIN0_SHIFT);
  452. SPEX(antenna_gain.a1, SSB_SPROM4_AGAIN01,
  453. SSB_SPROM4_AGAIN1, SSB_SPROM4_AGAIN1_SHIFT);
  454. SPEX(antenna_gain.a2, SSB_SPROM4_AGAIN23,
  455. SSB_SPROM4_AGAIN2, SSB_SPROM4_AGAIN2_SHIFT);
  456. SPEX(antenna_gain.a3, SSB_SPROM4_AGAIN23,
  457. SSB_SPROM4_AGAIN3, SSB_SPROM4_AGAIN3_SHIFT);
  458. sprom_extract_r458(out, in);
  459. /* TODO - get remaining rev 4 stuff needed */
  460. }
  461. static void sprom_extract_r8(struct ssb_sprom *out, const u16 *in)
  462. {
  463. int i;
  464. u16 v, o;
  465. u16 pwr_info_offset[] = {
  466. SSB_SROM8_PWR_INFO_CORE0, SSB_SROM8_PWR_INFO_CORE1,
  467. SSB_SROM8_PWR_INFO_CORE2, SSB_SROM8_PWR_INFO_CORE3
  468. };
  469. BUILD_BUG_ON(ARRAY_SIZE(pwr_info_offset) !=
  470. ARRAY_SIZE(out->core_pwr_info));
  471. /* extract the MAC address */
  472. for (i = 0; i < 3; i++) {
  473. v = in[SPOFF(SSB_SPROM8_IL0MAC) + i];
  474. *(((__be16 *)out->il0mac) + i) = cpu_to_be16(v);
  475. }
  476. SPEX(country_code, SSB_SPROM8_CCODE, 0xFFFF, 0);
  477. SPEX(boardflags_lo, SSB_SPROM8_BFLLO, 0xFFFF, 0);
  478. SPEX(boardflags_hi, SSB_SPROM8_BFLHI, 0xFFFF, 0);
  479. SPEX(boardflags2_lo, SSB_SPROM8_BFL2LO, 0xFFFF, 0);
  480. SPEX(boardflags2_hi, SSB_SPROM8_BFL2HI, 0xFFFF, 0);
  481. SPEX(ant_available_a, SSB_SPROM8_ANTAVAIL, SSB_SPROM8_ANTAVAIL_A,
  482. SSB_SPROM8_ANTAVAIL_A_SHIFT);
  483. SPEX(ant_available_bg, SSB_SPROM8_ANTAVAIL, SSB_SPROM8_ANTAVAIL_BG,
  484. SSB_SPROM8_ANTAVAIL_BG_SHIFT);
  485. SPEX(maxpwr_bg, SSB_SPROM8_MAXP_BG, SSB_SPROM8_MAXP_BG_MASK, 0);
  486. SPEX(itssi_bg, SSB_SPROM8_MAXP_BG, SSB_SPROM8_ITSSI_BG,
  487. SSB_SPROM8_ITSSI_BG_SHIFT);
  488. SPEX(maxpwr_a, SSB_SPROM8_MAXP_A, SSB_SPROM8_MAXP_A_MASK, 0);
  489. SPEX(itssi_a, SSB_SPROM8_MAXP_A, SSB_SPROM8_ITSSI_A,
  490. SSB_SPROM8_ITSSI_A_SHIFT);
  491. SPEX(maxpwr_ah, SSB_SPROM8_MAXP_AHL, SSB_SPROM8_MAXP_AH_MASK, 0);
  492. SPEX(maxpwr_al, SSB_SPROM8_MAXP_AHL, SSB_SPROM8_MAXP_AL_MASK,
  493. SSB_SPROM8_MAXP_AL_SHIFT);
  494. SPEX(gpio0, SSB_SPROM8_GPIOA, SSB_SPROM8_GPIOA_P0, 0);
  495. SPEX(gpio1, SSB_SPROM8_GPIOA, SSB_SPROM8_GPIOA_P1,
  496. SSB_SPROM8_GPIOA_P1_SHIFT);
  497. SPEX(gpio2, SSB_SPROM8_GPIOB, SSB_SPROM8_GPIOB_P2, 0);
  498. SPEX(gpio3, SSB_SPROM8_GPIOB, SSB_SPROM8_GPIOB_P3,
  499. SSB_SPROM8_GPIOB_P3_SHIFT);
  500. SPEX(tri2g, SSB_SPROM8_TRI25G, SSB_SPROM8_TRI2G, 0);
  501. SPEX(tri5g, SSB_SPROM8_TRI25G, SSB_SPROM8_TRI5G,
  502. SSB_SPROM8_TRI5G_SHIFT);
  503. SPEX(tri5gl, SSB_SPROM8_TRI5GHL, SSB_SPROM8_TRI5GL, 0);
  504. SPEX(tri5gh, SSB_SPROM8_TRI5GHL, SSB_SPROM8_TRI5GH,
  505. SSB_SPROM8_TRI5GH_SHIFT);
  506. SPEX(rxpo2g, SSB_SPROM8_RXPO, SSB_SPROM8_RXPO2G, 0);
  507. SPEX(rxpo5g, SSB_SPROM8_RXPO, SSB_SPROM8_RXPO5G,
  508. SSB_SPROM8_RXPO5G_SHIFT);
  509. SPEX(rssismf2g, SSB_SPROM8_RSSIPARM2G, SSB_SPROM8_RSSISMF2G, 0);
  510. SPEX(rssismc2g, SSB_SPROM8_RSSIPARM2G, SSB_SPROM8_RSSISMC2G,
  511. SSB_SPROM8_RSSISMC2G_SHIFT);
  512. SPEX(rssisav2g, SSB_SPROM8_RSSIPARM2G, SSB_SPROM8_RSSISAV2G,
  513. SSB_SPROM8_RSSISAV2G_SHIFT);
  514. SPEX(bxa2g, SSB_SPROM8_RSSIPARM2G, SSB_SPROM8_BXA2G,
  515. SSB_SPROM8_BXA2G_SHIFT);
  516. SPEX(rssismf5g, SSB_SPROM8_RSSIPARM5G, SSB_SPROM8_RSSISMF5G, 0);
  517. SPEX(rssismc5g, SSB_SPROM8_RSSIPARM5G, SSB_SPROM8_RSSISMC5G,
  518. SSB_SPROM8_RSSISMC5G_SHIFT);
  519. SPEX(rssisav5g, SSB_SPROM8_RSSIPARM5G, SSB_SPROM8_RSSISAV5G,
  520. SSB_SPROM8_RSSISAV5G_SHIFT);
  521. SPEX(bxa5g, SSB_SPROM8_RSSIPARM5G, SSB_SPROM8_BXA5G,
  522. SSB_SPROM8_BXA5G_SHIFT);
  523. SPEX(pa0b0, SSB_SPROM8_PA0B0, 0xFFFF, 0);
  524. SPEX(pa0b1, SSB_SPROM8_PA0B1, 0xFFFF, 0);
  525. SPEX(pa0b2, SSB_SPROM8_PA0B2, 0xFFFF, 0);
  526. SPEX(pa1b0, SSB_SPROM8_PA1B0, 0xFFFF, 0);
  527. SPEX(pa1b1, SSB_SPROM8_PA1B1, 0xFFFF, 0);
  528. SPEX(pa1b2, SSB_SPROM8_PA1B2, 0xFFFF, 0);
  529. SPEX(pa1lob0, SSB_SPROM8_PA1LOB0, 0xFFFF, 0);
  530. SPEX(pa1lob1, SSB_SPROM8_PA1LOB1, 0xFFFF, 0);
  531. SPEX(pa1lob2, SSB_SPROM8_PA1LOB2, 0xFFFF, 0);
  532. SPEX(pa1hib0, SSB_SPROM8_PA1HIB0, 0xFFFF, 0);
  533. SPEX(pa1hib1, SSB_SPROM8_PA1HIB1, 0xFFFF, 0);
  534. SPEX(pa1hib2, SSB_SPROM8_PA1HIB2, 0xFFFF, 0);
  535. SPEX(cck2gpo, SSB_SPROM8_CCK2GPO, 0xFFFF, 0);
  536. SPEX32(ofdm2gpo, SSB_SPROM8_OFDM2GPO, 0xFFFFFFFF, 0);
  537. SPEX32(ofdm5glpo, SSB_SPROM8_OFDM5GLPO, 0xFFFFFFFF, 0);
  538. SPEX32(ofdm5gpo, SSB_SPROM8_OFDM5GPO, 0xFFFFFFFF, 0);
  539. SPEX32(ofdm5ghpo, SSB_SPROM8_OFDM5GHPO, 0xFFFFFFFF, 0);
  540. /* Extract the antenna gain values. */
  541. SPEX(antenna_gain.a0, SSB_SPROM8_AGAIN01,
  542. SSB_SPROM8_AGAIN0, SSB_SPROM8_AGAIN0_SHIFT);
  543. SPEX(antenna_gain.a1, SSB_SPROM8_AGAIN01,
  544. SSB_SPROM8_AGAIN1, SSB_SPROM8_AGAIN1_SHIFT);
  545. SPEX(antenna_gain.a2, SSB_SPROM8_AGAIN23,
  546. SSB_SPROM8_AGAIN2, SSB_SPROM8_AGAIN2_SHIFT);
  547. SPEX(antenna_gain.a3, SSB_SPROM8_AGAIN23,
  548. SSB_SPROM8_AGAIN3, SSB_SPROM8_AGAIN3_SHIFT);
  549. /* Extract cores power info info */
  550. for (i = 0; i < ARRAY_SIZE(pwr_info_offset); i++) {
  551. o = pwr_info_offset[i];
  552. SPEX(core_pwr_info[i].itssi_2g, o + SSB_SROM8_2G_MAXP_ITSSI,
  553. SSB_SPROM8_2G_ITSSI, SSB_SPROM8_2G_ITSSI_SHIFT);
  554. SPEX(core_pwr_info[i].maxpwr_2g, o + SSB_SROM8_2G_MAXP_ITSSI,
  555. SSB_SPROM8_2G_MAXP, 0);
  556. SPEX(core_pwr_info[i].pa_2g[0], o + SSB_SROM8_2G_PA_0, ~0, 0);
  557. SPEX(core_pwr_info[i].pa_2g[1], o + SSB_SROM8_2G_PA_1, ~0, 0);
  558. SPEX(core_pwr_info[i].pa_2g[2], o + SSB_SROM8_2G_PA_2, ~0, 0);
  559. SPEX(core_pwr_info[i].itssi_5g, o + SSB_SROM8_5G_MAXP_ITSSI,
  560. SSB_SPROM8_5G_ITSSI, SSB_SPROM8_5G_ITSSI_SHIFT);
  561. SPEX(core_pwr_info[i].maxpwr_5g, o + SSB_SROM8_5G_MAXP_ITSSI,
  562. SSB_SPROM8_5G_MAXP, 0);
  563. SPEX(core_pwr_info[i].maxpwr_5gh, o + SSB_SPROM8_5GHL_MAXP,
  564. SSB_SPROM8_5GH_MAXP, 0);
  565. SPEX(core_pwr_info[i].maxpwr_5gl, o + SSB_SPROM8_5GHL_MAXP,
  566. SSB_SPROM8_5GL_MAXP, SSB_SPROM8_5GL_MAXP_SHIFT);
  567. SPEX(core_pwr_info[i].pa_5gl[0], o + SSB_SROM8_5GL_PA_0, ~0, 0);
  568. SPEX(core_pwr_info[i].pa_5gl[1], o + SSB_SROM8_5GL_PA_1, ~0, 0);
  569. SPEX(core_pwr_info[i].pa_5gl[2], o + SSB_SROM8_5GL_PA_2, ~0, 0);
  570. SPEX(core_pwr_info[i].pa_5g[0], o + SSB_SROM8_5G_PA_0, ~0, 0);
  571. SPEX(core_pwr_info[i].pa_5g[1], o + SSB_SROM8_5G_PA_1, ~0, 0);
  572. SPEX(core_pwr_info[i].pa_5g[2], o + SSB_SROM8_5G_PA_2, ~0, 0);
  573. SPEX(core_pwr_info[i].pa_5gh[0], o + SSB_SROM8_5GH_PA_0, ~0, 0);
  574. SPEX(core_pwr_info[i].pa_5gh[1], o + SSB_SROM8_5GH_PA_1, ~0, 0);
  575. SPEX(core_pwr_info[i].pa_5gh[2], o + SSB_SROM8_5GH_PA_2, ~0, 0);
  576. }
  577. /* Extract FEM info */
  578. SPEX(fem.ghz2.tssipos, SSB_SPROM8_FEM2G,
  579. SSB_SROM8_FEM_TSSIPOS, SSB_SROM8_FEM_TSSIPOS_SHIFT);
  580. SPEX(fem.ghz2.extpa_gain, SSB_SPROM8_FEM2G,
  581. SSB_SROM8_FEM_EXTPA_GAIN, SSB_SROM8_FEM_EXTPA_GAIN_SHIFT);
  582. SPEX(fem.ghz2.pdet_range, SSB_SPROM8_FEM2G,
  583. SSB_SROM8_FEM_PDET_RANGE, SSB_SROM8_FEM_PDET_RANGE_SHIFT);
  584. SPEX(fem.ghz2.tr_iso, SSB_SPROM8_FEM2G,
  585. SSB_SROM8_FEM_TR_ISO, SSB_SROM8_FEM_TR_ISO_SHIFT);
  586. SPEX(fem.ghz2.antswlut, SSB_SPROM8_FEM2G,
  587. SSB_SROM8_FEM_ANTSWLUT, SSB_SROM8_FEM_ANTSWLUT_SHIFT);
  588. SPEX(fem.ghz5.tssipos, SSB_SPROM8_FEM5G,
  589. SSB_SROM8_FEM_TSSIPOS, SSB_SROM8_FEM_TSSIPOS_SHIFT);
  590. SPEX(fem.ghz5.extpa_gain, SSB_SPROM8_FEM5G,
  591. SSB_SROM8_FEM_EXTPA_GAIN, SSB_SROM8_FEM_EXTPA_GAIN_SHIFT);
  592. SPEX(fem.ghz5.pdet_range, SSB_SPROM8_FEM5G,
  593. SSB_SROM8_FEM_PDET_RANGE, SSB_SROM8_FEM_PDET_RANGE_SHIFT);
  594. SPEX(fem.ghz5.tr_iso, SSB_SPROM8_FEM5G,
  595. SSB_SROM8_FEM_TR_ISO, SSB_SROM8_FEM_TR_ISO_SHIFT);
  596. SPEX(fem.ghz5.antswlut, SSB_SPROM8_FEM5G,
  597. SSB_SROM8_FEM_ANTSWLUT, SSB_SROM8_FEM_ANTSWLUT_SHIFT);
  598. sprom_extract_r458(out, in);
  599. /* TODO - get remaining rev 8 stuff needed */
  600. }
  601. static int sprom_extract(struct ssb_bus *bus, struct ssb_sprom *out,
  602. const u16 *in, u16 size)
  603. {
  604. memset(out, 0, sizeof(*out));
  605. out->revision = in[size - 1] & 0x00FF;
  606. ssb_dprintk(KERN_DEBUG PFX "SPROM revision %d detected.\n", out->revision);
  607. memset(out->et0mac, 0xFF, 6); /* preset et0 and et1 mac */
  608. memset(out->et1mac, 0xFF, 6);
  609. if ((bus->chip_id & 0xFF00) == 0x4400) {
  610. /* Workaround: The BCM44XX chip has a stupid revision
  611. * number stored in the SPROM.
  612. * Always extract r1. */
  613. out->revision = 1;
  614. ssb_dprintk(KERN_DEBUG PFX "SPROM treated as revision %d\n", out->revision);
  615. }
  616. switch (out->revision) {
  617. case 1:
  618. case 2:
  619. case 3:
  620. sprom_extract_r123(out, in);
  621. break;
  622. case 4:
  623. case 5:
  624. sprom_extract_r45(out, in);
  625. break;
  626. case 8:
  627. sprom_extract_r8(out, in);
  628. break;
  629. default:
  630. ssb_printk(KERN_WARNING PFX "Unsupported SPROM"
  631. " revision %d detected. Will extract"
  632. " v1\n", out->revision);
  633. out->revision = 1;
  634. sprom_extract_r123(out, in);
  635. }
  636. if (out->boardflags_lo == 0xFFFF)
  637. out->boardflags_lo = 0; /* per specs */
  638. if (out->boardflags_hi == 0xFFFF)
  639. out->boardflags_hi = 0; /* per specs */
  640. return 0;
  641. }
  642. static int ssb_pci_sprom_get(struct ssb_bus *bus,
  643. struct ssb_sprom *sprom)
  644. {
  645. int err;
  646. u16 *buf;
  647. if (!ssb_is_sprom_available(bus)) {
  648. ssb_printk(KERN_ERR PFX "No SPROM available!\n");
  649. return -ENODEV;
  650. }
  651. if (bus->chipco.dev) { /* can be unavailable! */
  652. /*
  653. * get SPROM offset: SSB_SPROM_BASE1 except for
  654. * chipcommon rev >= 31 or chip ID is 0x4312 and
  655. * chipcommon status & 3 == 2
  656. */
  657. if (bus->chipco.dev->id.revision >= 31)
  658. bus->sprom_offset = SSB_SPROM_BASE31;
  659. else if (bus->chip_id == 0x4312 &&
  660. (bus->chipco.status & 0x03) == 2)
  661. bus->sprom_offset = SSB_SPROM_BASE31;
  662. else
  663. bus->sprom_offset = SSB_SPROM_BASE1;
  664. } else {
  665. bus->sprom_offset = SSB_SPROM_BASE1;
  666. }
  667. ssb_dprintk(KERN_INFO PFX "SPROM offset is 0x%x\n", bus->sprom_offset);
  668. buf = kcalloc(SSB_SPROMSIZE_WORDS_R123, sizeof(u16), GFP_KERNEL);
  669. if (!buf)
  670. return -ENOMEM;
  671. bus->sprom_size = SSB_SPROMSIZE_WORDS_R123;
  672. sprom_do_read(bus, buf);
  673. err = sprom_check_crc(buf, bus->sprom_size);
  674. if (err) {
  675. /* try for a 440 byte SPROM - revision 4 and higher */
  676. kfree(buf);
  677. buf = kcalloc(SSB_SPROMSIZE_WORDS_R4, sizeof(u16),
  678. GFP_KERNEL);
  679. if (!buf)
  680. return -ENOMEM;
  681. bus->sprom_size = SSB_SPROMSIZE_WORDS_R4;
  682. sprom_do_read(bus, buf);
  683. err = sprom_check_crc(buf, bus->sprom_size);
  684. if (err) {
  685. /* All CRC attempts failed.
  686. * Maybe there is no SPROM on the device?
  687. * Now we ask the arch code if there is some sprom
  688. * available for this device in some other storage */
  689. err = ssb_fill_sprom_with_fallback(bus, sprom);
  690. if (err) {
  691. ssb_printk(KERN_WARNING PFX "WARNING: Using"
  692. " fallback SPROM failed (err %d)\n",
  693. err);
  694. } else {
  695. ssb_dprintk(KERN_DEBUG PFX "Using SPROM"
  696. " revision %d provided by"
  697. " platform.\n", sprom->revision);
  698. err = 0;
  699. goto out_free;
  700. }
  701. ssb_printk(KERN_WARNING PFX "WARNING: Invalid"
  702. " SPROM CRC (corrupt SPROM)\n");
  703. }
  704. }
  705. err = sprom_extract(bus, sprom, buf, bus->sprom_size);
  706. out_free:
  707. kfree(buf);
  708. return err;
  709. }
  710. static void ssb_pci_get_boardinfo(struct ssb_bus *bus,
  711. struct ssb_boardinfo *bi)
  712. {
  713. bi->vendor = bus->host_pci->subsystem_vendor;
  714. bi->type = bus->host_pci->subsystem_device;
  715. bi->rev = bus->host_pci->revision;
  716. }
  717. int ssb_pci_get_invariants(struct ssb_bus *bus,
  718. struct ssb_init_invariants *iv)
  719. {
  720. int err;
  721. err = ssb_pci_sprom_get(bus, &iv->sprom);
  722. if (err)
  723. goto out;
  724. ssb_pci_get_boardinfo(bus, &iv->boardinfo);
  725. out:
  726. return err;
  727. }
  728. #ifdef CONFIG_SSB_DEBUG
  729. static int ssb_pci_assert_buspower(struct ssb_bus *bus)
  730. {
  731. if (likely(bus->powered_up))
  732. return 0;
  733. printk(KERN_ERR PFX "FATAL ERROR: Bus powered down "
  734. "while accessing PCI MMIO space\n");
  735. if (bus->power_warn_count <= 10) {
  736. bus->power_warn_count++;
  737. dump_stack();
  738. }
  739. return -ENODEV;
  740. }
  741. #else /* DEBUG */
  742. static inline int ssb_pci_assert_buspower(struct ssb_bus *bus)
  743. {
  744. return 0;
  745. }
  746. #endif /* DEBUG */
  747. static u8 ssb_pci_read8(struct ssb_device *dev, u16 offset)
  748. {
  749. struct ssb_bus *bus = dev->bus;
  750. if (unlikely(ssb_pci_assert_buspower(bus)))
  751. return 0xFF;
  752. if (unlikely(bus->mapped_device != dev)) {
  753. if (unlikely(ssb_pci_switch_core(bus, dev)))
  754. return 0xFF;
  755. }
  756. return ioread8(bus->mmio + offset);
  757. }
  758. static u16 ssb_pci_read16(struct ssb_device *dev, u16 offset)
  759. {
  760. struct ssb_bus *bus = dev->bus;
  761. if (unlikely(ssb_pci_assert_buspower(bus)))
  762. return 0xFFFF;
  763. if (unlikely(bus->mapped_device != dev)) {
  764. if (unlikely(ssb_pci_switch_core(bus, dev)))
  765. return 0xFFFF;
  766. }
  767. return ioread16(bus->mmio + offset);
  768. }
  769. static u32 ssb_pci_read32(struct ssb_device *dev, u16 offset)
  770. {
  771. struct ssb_bus *bus = dev->bus;
  772. if (unlikely(ssb_pci_assert_buspower(bus)))
  773. return 0xFFFFFFFF;
  774. if (unlikely(bus->mapped_device != dev)) {
  775. if (unlikely(ssb_pci_switch_core(bus, dev)))
  776. return 0xFFFFFFFF;
  777. }
  778. return ioread32(bus->mmio + offset);
  779. }
  780. #ifdef CONFIG_SSB_BLOCKIO
  781. static void ssb_pci_block_read(struct ssb_device *dev, void *buffer,
  782. size_t count, u16 offset, u8 reg_width)
  783. {
  784. struct ssb_bus *bus = dev->bus;
  785. void __iomem *addr = bus->mmio + offset;
  786. if (unlikely(ssb_pci_assert_buspower(bus)))
  787. goto error;
  788. if (unlikely(bus->mapped_device != dev)) {
  789. if (unlikely(ssb_pci_switch_core(bus, dev)))
  790. goto error;
  791. }
  792. switch (reg_width) {
  793. case sizeof(u8):
  794. ioread8_rep(addr, buffer, count);
  795. break;
  796. case sizeof(u16):
  797. SSB_WARN_ON(count & 1);
  798. ioread16_rep(addr, buffer, count >> 1);
  799. break;
  800. case sizeof(u32):
  801. SSB_WARN_ON(count & 3);
  802. ioread32_rep(addr, buffer, count >> 2);
  803. break;
  804. default:
  805. SSB_WARN_ON(1);
  806. }
  807. return;
  808. error:
  809. memset(buffer, 0xFF, count);
  810. }
  811. #endif /* CONFIG_SSB_BLOCKIO */
  812. static void ssb_pci_write8(struct ssb_device *dev, u16 offset, u8 value)
  813. {
  814. struct ssb_bus *bus = dev->bus;
  815. if (unlikely(ssb_pci_assert_buspower(bus)))
  816. return;
  817. if (unlikely(bus->mapped_device != dev)) {
  818. if (unlikely(ssb_pci_switch_core(bus, dev)))
  819. return;
  820. }
  821. iowrite8(value, bus->mmio + offset);
  822. }
  823. static void ssb_pci_write16(struct ssb_device *dev, u16 offset, u16 value)
  824. {
  825. struct ssb_bus *bus = dev->bus;
  826. if (unlikely(ssb_pci_assert_buspower(bus)))
  827. return;
  828. if (unlikely(bus->mapped_device != dev)) {
  829. if (unlikely(ssb_pci_switch_core(bus, dev)))
  830. return;
  831. }
  832. iowrite16(value, bus->mmio + offset);
  833. }
  834. static void ssb_pci_write32(struct ssb_device *dev, u16 offset, u32 value)
  835. {
  836. struct ssb_bus *bus = dev->bus;
  837. if (unlikely(ssb_pci_assert_buspower(bus)))
  838. return;
  839. if (unlikely(bus->mapped_device != dev)) {
  840. if (unlikely(ssb_pci_switch_core(bus, dev)))
  841. return;
  842. }
  843. iowrite32(value, bus->mmio + offset);
  844. }
  845. #ifdef CONFIG_SSB_BLOCKIO
  846. static void ssb_pci_block_write(struct ssb_device *dev, const void *buffer,
  847. size_t count, u16 offset, u8 reg_width)
  848. {
  849. struct ssb_bus *bus = dev->bus;
  850. void __iomem *addr = bus->mmio + offset;
  851. if (unlikely(ssb_pci_assert_buspower(bus)))
  852. return;
  853. if (unlikely(bus->mapped_device != dev)) {
  854. if (unlikely(ssb_pci_switch_core(bus, dev)))
  855. return;
  856. }
  857. switch (reg_width) {
  858. case sizeof(u8):
  859. iowrite8_rep(addr, buffer, count);
  860. break;
  861. case sizeof(u16):
  862. SSB_WARN_ON(count & 1);
  863. iowrite16_rep(addr, buffer, count >> 1);
  864. break;
  865. case sizeof(u32):
  866. SSB_WARN_ON(count & 3);
  867. iowrite32_rep(addr, buffer, count >> 2);
  868. break;
  869. default:
  870. SSB_WARN_ON(1);
  871. }
  872. }
  873. #endif /* CONFIG_SSB_BLOCKIO */
  874. /* Not "static", as it's used in main.c */
  875. const struct ssb_bus_ops ssb_pci_ops = {
  876. .read8 = ssb_pci_read8,
  877. .read16 = ssb_pci_read16,
  878. .read32 = ssb_pci_read32,
  879. .write8 = ssb_pci_write8,
  880. .write16 = ssb_pci_write16,
  881. .write32 = ssb_pci_write32,
  882. #ifdef CONFIG_SSB_BLOCKIO
  883. .block_read = ssb_pci_block_read,
  884. .block_write = ssb_pci_block_write,
  885. #endif
  886. };
  887. static ssize_t ssb_pci_attr_sprom_show(struct device *pcidev,
  888. struct device_attribute *attr,
  889. char *buf)
  890. {
  891. struct pci_dev *pdev = container_of(pcidev, struct pci_dev, dev);
  892. struct ssb_bus *bus;
  893. bus = ssb_pci_dev_to_bus(pdev);
  894. if (!bus)
  895. return -ENODEV;
  896. return ssb_attr_sprom_show(bus, buf, sprom_do_read);
  897. }
  898. static ssize_t ssb_pci_attr_sprom_store(struct device *pcidev,
  899. struct device_attribute *attr,
  900. const char *buf, size_t count)
  901. {
  902. struct pci_dev *pdev = container_of(pcidev, struct pci_dev, dev);
  903. struct ssb_bus *bus;
  904. bus = ssb_pci_dev_to_bus(pdev);
  905. if (!bus)
  906. return -ENODEV;
  907. return ssb_attr_sprom_store(bus, buf, count,
  908. sprom_check_crc, sprom_do_write);
  909. }
  910. static DEVICE_ATTR(ssb_sprom, 0600,
  911. ssb_pci_attr_sprom_show,
  912. ssb_pci_attr_sprom_store);
  913. void ssb_pci_exit(struct ssb_bus *bus)
  914. {
  915. struct pci_dev *pdev;
  916. if (bus->bustype != SSB_BUSTYPE_PCI)
  917. return;
  918. pdev = bus->host_pci;
  919. device_remove_file(&pdev->dev, &dev_attr_ssb_sprom);
  920. }
  921. int ssb_pci_init(struct ssb_bus *bus)
  922. {
  923. struct pci_dev *pdev;
  924. int err;
  925. if (bus->bustype != SSB_BUSTYPE_PCI)
  926. return 0;
  927. pdev = bus->host_pci;
  928. mutex_init(&bus->sprom_mutex);
  929. err = device_create_file(&pdev->dev, &dev_attr_ssb_sprom);
  930. if (err)
  931. goto out;
  932. out:
  933. return err;
  934. }