ip22-gio.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/export.h>
  3. #include <linux/kernel.h>
  4. #include <linux/init.h>
  5. #include <linux/slab.h>
  6. #include <asm/addrspace.h>
  7. #include <asm/paccess.h>
  8. #include <asm/gio_device.h>
  9. #include <asm/sgi/gio.h>
  10. #include <asm/sgi/hpc3.h>
  11. #include <asm/sgi/mc.h>
  12. #include <asm/sgi/ip22.h>
  13. static const struct bus_type gio_bus_type;
  14. static struct {
  15. const char *name;
  16. __u8 id;
  17. } gio_name_table[] = {
  18. { .name = "SGI Impact", .id = 0x10 },
  19. { .name = "Phobos G160", .id = 0x35 },
  20. { .name = "Phobos G130", .id = 0x36 },
  21. { .name = "Phobos G100", .id = 0x37 },
  22. { .name = "Set Engineering GFE", .id = 0x38 },
  23. /* fake IDs */
  24. { .name = "SGI Newport", .id = 0x7e },
  25. { .name = "SGI GR2/GR3", .id = 0x7f },
  26. };
  27. static void gio_bus_release(struct device *dev)
  28. {
  29. kfree(dev);
  30. }
  31. static struct device gio_bus = {
  32. .init_name = "gio",
  33. .release = &gio_bus_release,
  34. };
  35. /**
  36. * gio_match_device - Tell if an of_device structure has a matching
  37. * gio_match structure
  38. * @ids: array of of device match structures to search in
  39. * @dev: the of device structure to match against
  40. *
  41. * Used by a driver to check whether an of_device present in the
  42. * system is in its list of supported devices.
  43. */
  44. static const struct gio_device_id *
  45. gio_match_device(const struct gio_device_id *match,
  46. const struct gio_device *dev)
  47. {
  48. const struct gio_device_id *ids;
  49. for (ids = match; ids->id != 0xff; ids++)
  50. if (ids->id == dev->id.id)
  51. return ids;
  52. return NULL;
  53. }
  54. struct gio_device *gio_dev_get(struct gio_device *dev)
  55. {
  56. struct device *tmp;
  57. if (!dev)
  58. return NULL;
  59. tmp = get_device(&dev->dev);
  60. if (tmp)
  61. return to_gio_device(tmp);
  62. else
  63. return NULL;
  64. }
  65. EXPORT_SYMBOL_GPL(gio_dev_get);
  66. void gio_dev_put(struct gio_device *dev)
  67. {
  68. if (dev)
  69. put_device(&dev->dev);
  70. }
  71. EXPORT_SYMBOL_GPL(gio_dev_put);
  72. /**
  73. * gio_release_dev - free an gio device structure when all users of it are finished.
  74. * @dev: device that's been disconnected
  75. *
  76. * Will be called only by the device core when all users of this gio device are
  77. * done.
  78. */
  79. void gio_release_dev(struct device *dev)
  80. {
  81. struct gio_device *giodev;
  82. giodev = to_gio_device(dev);
  83. kfree(giodev);
  84. }
  85. EXPORT_SYMBOL_GPL(gio_release_dev);
  86. int gio_device_register(struct gio_device *giodev)
  87. {
  88. giodev->dev.bus = &gio_bus_type;
  89. giodev->dev.parent = &gio_bus;
  90. return device_register(&giodev->dev);
  91. }
  92. EXPORT_SYMBOL_GPL(gio_device_register);
  93. void gio_device_unregister(struct gio_device *giodev)
  94. {
  95. device_unregister(&giodev->dev);
  96. }
  97. EXPORT_SYMBOL_GPL(gio_device_unregister);
  98. static int gio_bus_match(struct device *dev, const struct device_driver *drv)
  99. {
  100. struct gio_device *gio_dev = to_gio_device(dev);
  101. struct gio_driver *gio_drv = to_gio_driver(drv);
  102. return gio_match_device(gio_drv->id_table, gio_dev) != NULL;
  103. }
  104. static int gio_device_probe(struct device *dev)
  105. {
  106. int error = -ENODEV;
  107. struct gio_driver *drv;
  108. struct gio_device *gio_dev;
  109. const struct gio_device_id *match;
  110. drv = to_gio_driver(dev->driver);
  111. gio_dev = to_gio_device(dev);
  112. if (!drv->probe)
  113. return error;
  114. gio_dev_get(gio_dev);
  115. match = gio_match_device(drv->id_table, gio_dev);
  116. if (match)
  117. error = drv->probe(gio_dev, match);
  118. if (error)
  119. gio_dev_put(gio_dev);
  120. return error;
  121. }
  122. static void gio_device_remove(struct device *dev)
  123. {
  124. struct gio_device *gio_dev = to_gio_device(dev);
  125. struct gio_driver *drv = to_gio_driver(dev->driver);
  126. if (drv->remove)
  127. drv->remove(gio_dev);
  128. }
  129. static void gio_device_shutdown(struct device *dev)
  130. {
  131. struct gio_device *gio_dev = to_gio_device(dev);
  132. struct gio_driver *drv = to_gio_driver(dev->driver);
  133. if (dev->driver && drv->shutdown)
  134. drv->shutdown(gio_dev);
  135. }
  136. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  137. char *buf)
  138. {
  139. struct gio_device *gio_dev = to_gio_device(dev);
  140. return sysfs_emit(buf, "gio:%x\n", gio_dev->id.id);
  141. }
  142. static DEVICE_ATTR_RO(modalias);
  143. static ssize_t name_show(struct device *dev,
  144. struct device_attribute *attr, char *buf)
  145. {
  146. struct gio_device *giodev;
  147. giodev = to_gio_device(dev);
  148. return sysfs_emit(buf, "%s\n", giodev->name);
  149. }
  150. static DEVICE_ATTR_RO(name);
  151. static ssize_t id_show(struct device *dev,
  152. struct device_attribute *attr, char *buf)
  153. {
  154. struct gio_device *giodev;
  155. giodev = to_gio_device(dev);
  156. return sysfs_emit(buf, "%x\n", giodev->id.id);
  157. }
  158. static DEVICE_ATTR_RO(id);
  159. static struct attribute *gio_dev_attrs[] = {
  160. &dev_attr_modalias.attr,
  161. &dev_attr_name.attr,
  162. &dev_attr_id.attr,
  163. NULL,
  164. };
  165. ATTRIBUTE_GROUPS(gio_dev);
  166. static int gio_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
  167. {
  168. const struct gio_device *gio_dev = to_gio_device(dev);
  169. add_uevent_var(env, "MODALIAS=gio:%x", gio_dev->id.id);
  170. return 0;
  171. }
  172. int gio_register_driver(struct gio_driver *drv)
  173. {
  174. /* initialize common driver fields */
  175. if (!drv->driver.name)
  176. drv->driver.name = drv->name;
  177. if (!drv->driver.owner)
  178. drv->driver.owner = drv->owner;
  179. drv->driver.bus = &gio_bus_type;
  180. /* register with core */
  181. return driver_register(&drv->driver);
  182. }
  183. EXPORT_SYMBOL_GPL(gio_register_driver);
  184. void gio_unregister_driver(struct gio_driver *drv)
  185. {
  186. driver_unregister(&drv->driver);
  187. }
  188. EXPORT_SYMBOL_GPL(gio_unregister_driver);
  189. void gio_set_master(struct gio_device *dev)
  190. {
  191. u32 tmp = sgimc->giopar;
  192. switch (dev->slotno) {
  193. case 0:
  194. tmp |= SGIMC_GIOPAR_MASTERGFX;
  195. break;
  196. case 1:
  197. tmp |= SGIMC_GIOPAR_MASTEREXP0;
  198. break;
  199. case 2:
  200. tmp |= SGIMC_GIOPAR_MASTEREXP1;
  201. break;
  202. }
  203. sgimc->giopar = tmp;
  204. }
  205. EXPORT_SYMBOL_GPL(gio_set_master);
  206. static void ip22_gio_set_64bit(int slotno)
  207. {
  208. u32 tmp = sgimc->giopar;
  209. switch (slotno) {
  210. case 0:
  211. tmp |= SGIMC_GIOPAR_GFX64;
  212. break;
  213. case 1:
  214. tmp |= SGIMC_GIOPAR_EXP064;
  215. break;
  216. case 2:
  217. tmp |= SGIMC_GIOPAR_EXP164;
  218. break;
  219. }
  220. sgimc->giopar = tmp;
  221. }
  222. static int ip22_gio_id(unsigned long addr, u32 *res)
  223. {
  224. u8 tmp8;
  225. u8 tmp16;
  226. u32 tmp32;
  227. u8 *ptr8;
  228. u16 *ptr16;
  229. u32 *ptr32;
  230. ptr32 = (void *)CKSEG1ADDR(addr);
  231. if (!get_dbe(tmp32, ptr32)) {
  232. /*
  233. * We got no DBE, but this doesn't mean anything.
  234. * If GIO is pipelined (which can't be disabled
  235. * for GFX slot) we don't get a DBE, but we see
  236. * the transfer size as data. So we do an 8bit
  237. * and a 16bit access and check whether the common
  238. * data matches
  239. */
  240. ptr8 = (void *)CKSEG1ADDR(addr + 3);
  241. if (get_dbe(tmp8, ptr8)) {
  242. /*
  243. * 32bit access worked, but 8bit doesn't
  244. * so we don't see phantom reads on
  245. * a pipelined bus, but a real card which
  246. * doesn't support 8 bit reads
  247. */
  248. *res = tmp32;
  249. return 1;
  250. }
  251. ptr16 = (void *)CKSEG1ADDR(addr + 2);
  252. get_dbe(tmp16, ptr16);
  253. if (tmp8 == (tmp16 & 0xff) &&
  254. tmp8 == (tmp32 & 0xff) &&
  255. tmp16 == (tmp32 & 0xffff)) {
  256. *res = tmp32;
  257. return 1;
  258. }
  259. }
  260. return 0; /* nothing here */
  261. }
  262. #define HQ2_MYSTERY_OFFS 0x6A07C
  263. #define NEWPORT_USTATUS_OFFS 0xF133C
  264. static int ip22_is_gr2(unsigned long addr)
  265. {
  266. u32 tmp;
  267. u32 *ptr;
  268. /* HQ2 only allows 32bit accesses */
  269. ptr = (void *)CKSEG1ADDR(addr + HQ2_MYSTERY_OFFS);
  270. if (!get_dbe(tmp, ptr)) {
  271. if (tmp == 0xdeadbeef)
  272. return 1;
  273. }
  274. return 0;
  275. }
  276. static void ip22_check_gio(int slotno, unsigned long addr, int irq)
  277. {
  278. const char *name = "Unknown";
  279. struct gio_device *gio_dev;
  280. u32 tmp;
  281. __u8 id;
  282. int i;
  283. /* first look for GR2/GR3 by checking mystery register */
  284. if (ip22_is_gr2(addr))
  285. tmp = 0x7f;
  286. else {
  287. if (!ip22_gio_id(addr, &tmp)) {
  288. /*
  289. * no GIO signature at start address of slot
  290. * since Newport doesn't have one, we check if
  291. * user status register is readable
  292. */
  293. if (ip22_gio_id(addr + NEWPORT_USTATUS_OFFS, &tmp))
  294. tmp = 0x7e;
  295. else
  296. tmp = 0;
  297. }
  298. }
  299. if (tmp) {
  300. id = GIO_ID(tmp);
  301. if (tmp & GIO_32BIT_ID) {
  302. if (tmp & GIO_64BIT_IFACE)
  303. ip22_gio_set_64bit(slotno);
  304. }
  305. for (i = 0; i < ARRAY_SIZE(gio_name_table); i++) {
  306. if (id == gio_name_table[i].id) {
  307. name = gio_name_table[i].name;
  308. break;
  309. }
  310. }
  311. printk(KERN_INFO "GIO: slot %d : %s (id %x)\n",
  312. slotno, name, id);
  313. gio_dev = kzalloc_obj(*gio_dev);
  314. if (!gio_dev)
  315. return;
  316. gio_dev->name = name;
  317. gio_dev->slotno = slotno;
  318. gio_dev->id.id = id;
  319. gio_dev->resource.start = addr;
  320. gio_dev->resource.end = addr + 0x3fffff;
  321. gio_dev->resource.flags = IORESOURCE_MEM;
  322. gio_dev->irq = irq;
  323. dev_set_name(&gio_dev->dev, "%d", slotno);
  324. if (gio_device_register(gio_dev))
  325. gio_dev_put(gio_dev);
  326. } else
  327. printk(KERN_INFO "GIO: slot %d : Empty\n", slotno);
  328. }
  329. static const struct bus_type gio_bus_type = {
  330. .name = "gio",
  331. .dev_groups = gio_dev_groups,
  332. .match = gio_bus_match,
  333. .probe = gio_device_probe,
  334. .remove = gio_device_remove,
  335. .shutdown = gio_device_shutdown,
  336. .uevent = gio_device_uevent,
  337. };
  338. static struct resource gio_bus_resource = {
  339. .start = GIO_SLOT_GFX_BASE,
  340. .end = GIO_SLOT_GFX_BASE + 0x9fffff,
  341. .name = "GIO Bus",
  342. .flags = IORESOURCE_MEM,
  343. };
  344. static int __init ip22_gio_init(void)
  345. {
  346. unsigned int pbdma __maybe_unused;
  347. int ret;
  348. ret = device_register(&gio_bus);
  349. if (ret) {
  350. put_device(&gio_bus);
  351. return ret;
  352. }
  353. ret = bus_register(&gio_bus_type);
  354. if (!ret) {
  355. request_resource(&iomem_resource, &gio_bus_resource);
  356. printk(KERN_INFO "GIO: Probing bus...\n");
  357. if (ip22_is_fullhouse()) {
  358. /* Indigo2 */
  359. ip22_check_gio(0, GIO_SLOT_GFX_BASE, SGI_GIO_1_IRQ);
  360. ip22_check_gio(1, GIO_SLOT_EXP0_BASE, SGI_GIO_1_IRQ);
  361. } else {
  362. /* Indy/Challenge S */
  363. if (get_dbe(pbdma, (unsigned int *)&hpc3c1->pbdma[1]))
  364. ip22_check_gio(0, GIO_SLOT_GFX_BASE,
  365. SGI_GIO_0_IRQ);
  366. ip22_check_gio(1, GIO_SLOT_EXP0_BASE, SGI_GIOEXP0_IRQ);
  367. ip22_check_gio(2, GIO_SLOT_EXP1_BASE, SGI_GIOEXP1_IRQ);
  368. }
  369. } else
  370. device_unregister(&gio_bus);
  371. return ret;
  372. }
  373. subsys_initcall(ip22_gio_init);