core_marvel.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. /*
  2. * linux/arch/alpha/kernel/core_marvel.c
  3. *
  4. * Code common to all Marvel based systems.
  5. */
  6. #define __EXTERN_INLINE inline
  7. #include <asm/io.h>
  8. #include <asm/core_marvel.h>
  9. #undef __EXTERN_INLINE
  10. #include <linux/types.h>
  11. #include <linux/pci.h>
  12. #include <linux/sched.h>
  13. #include <linux/init.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/mc146818rtc.h>
  16. #include <linux/rtc.h>
  17. #include <linux/module.h>
  18. #include <linux/bootmem.h>
  19. #include <asm/ptrace.h>
  20. #include <asm/smp.h>
  21. #include <asm/gct.h>
  22. #include <asm/pgalloc.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/rtc.h>
  25. #include <asm/vga.h>
  26. #include "proto.h"
  27. #include "pci_impl.h"
  28. /*
  29. * Debug helpers
  30. */
  31. #define DEBUG_CONFIG 0
  32. #if DEBUG_CONFIG
  33. # define DBG_CFG(args) printk args
  34. #else
  35. # define DBG_CFG(args)
  36. #endif
  37. /*
  38. * Private data
  39. */
  40. static struct io7 *io7_head = NULL;
  41. /*
  42. * Helper functions
  43. */
  44. static unsigned long __attribute__ ((unused))
  45. read_ev7_csr(int pe, unsigned long offset)
  46. {
  47. ev7_csr *ev7csr = EV7_CSR_KERN(pe, offset);
  48. unsigned long q;
  49. mb();
  50. q = ev7csr->csr;
  51. mb();
  52. return q;
  53. }
  54. static void __attribute__ ((unused))
  55. write_ev7_csr(int pe, unsigned long offset, unsigned long q)
  56. {
  57. ev7_csr *ev7csr = EV7_CSR_KERN(pe, offset);
  58. mb();
  59. ev7csr->csr = q;
  60. mb();
  61. }
  62. static char * __init
  63. mk_resource_name(int pe, int port, char *str)
  64. {
  65. char tmp[80];
  66. char *name;
  67. sprintf(tmp, "PCI %s PE %d PORT %d", str, pe, port);
  68. name = alloc_bootmem(strlen(tmp) + 1);
  69. strcpy(name, tmp);
  70. return name;
  71. }
  72. inline struct io7 *
  73. marvel_next_io7(struct io7 *prev)
  74. {
  75. return (prev ? prev->next : io7_head);
  76. }
  77. struct io7 *
  78. marvel_find_io7(int pe)
  79. {
  80. struct io7 *io7;
  81. for (io7 = io7_head; io7 && io7->pe != pe; io7 = io7->next)
  82. continue;
  83. return io7;
  84. }
  85. static struct io7 * __init
  86. alloc_io7(unsigned int pe)
  87. {
  88. struct io7 *io7;
  89. struct io7 *insp;
  90. int h;
  91. if (marvel_find_io7(pe)) {
  92. printk(KERN_WARNING "IO7 at PE %d already allocated!\n", pe);
  93. return NULL;
  94. }
  95. io7 = alloc_bootmem(sizeof(*io7));
  96. io7->pe = pe;
  97. spin_lock_init(&io7->irq_lock);
  98. for (h = 0; h < 4; h++) {
  99. io7->ports[h].io7 = io7;
  100. io7->ports[h].port = h;
  101. io7->ports[h].enabled = 0; /* default to disabled */
  102. }
  103. /*
  104. * Insert in pe sorted order.
  105. */
  106. if (NULL == io7_head) /* empty list */
  107. io7_head = io7;
  108. else if (io7_head->pe > io7->pe) { /* insert at head */
  109. io7->next = io7_head;
  110. io7_head = io7;
  111. } else { /* insert at position */
  112. for (insp = io7_head; insp; insp = insp->next) {
  113. if (insp->pe == io7->pe) {
  114. printk(KERN_ERR "Too many IO7s at PE %d\n",
  115. io7->pe);
  116. return NULL;
  117. }
  118. if (NULL == insp->next ||
  119. insp->next->pe > io7->pe) { /* insert here */
  120. io7->next = insp->next;
  121. insp->next = io7;
  122. break;
  123. }
  124. }
  125. if (NULL == insp) { /* couldn't insert ?!? */
  126. printk(KERN_WARNING "Failed to insert IO7 at PE %d "
  127. " - adding at head of list\n", io7->pe);
  128. io7->next = io7_head;
  129. io7_head = io7;
  130. }
  131. }
  132. return io7;
  133. }
  134. void
  135. io7_clear_errors(struct io7 *io7)
  136. {
  137. io7_port7_csrs *p7csrs;
  138. io7_ioport_csrs *csrs;
  139. int port;
  140. /*
  141. * First the IO ports.
  142. */
  143. for (port = 0; port < 4; port++) {
  144. csrs = IO7_CSRS_KERN(io7->pe, port);
  145. csrs->POx_ERR_SUM.csr = -1UL;
  146. csrs->POx_TLB_ERR.csr = -1UL;
  147. csrs->POx_SPL_COMPLT.csr = -1UL;
  148. csrs->POx_TRANS_SUM.csr = -1UL;
  149. }
  150. /*
  151. * Then the common ones.
  152. */
  153. p7csrs = IO7_PORT7_CSRS_KERN(io7->pe);
  154. p7csrs->PO7_ERROR_SUM.csr = -1UL;
  155. p7csrs->PO7_UNCRR_SYM.csr = -1UL;
  156. p7csrs->PO7_CRRCT_SYM.csr = -1UL;
  157. }
  158. /*
  159. * IO7 PCI, PCI/X, AGP configuration.
  160. */
  161. static void __init
  162. io7_init_hose(struct io7 *io7, int port)
  163. {
  164. static int hose_index = 0;
  165. struct pci_controller *hose = alloc_pci_controller();
  166. struct io7_port *io7_port = &io7->ports[port];
  167. io7_ioport_csrs *csrs = IO7_CSRS_KERN(io7->pe, port);
  168. int i;
  169. hose->index = hose_index++; /* arbitrary */
  170. /*
  171. * We don't have an isa or legacy hose, but glibc expects to be
  172. * able to use the bus == 0 / dev == 0 form of the iobase syscall
  173. * to determine information about the i/o system. Since XFree86
  174. * relies on glibc's determination to tell whether or not to use
  175. * sparse access, we need to point the pci_isa_hose at a real hose
  176. * so at least that determination is correct.
  177. */
  178. if (hose->index == 0)
  179. pci_isa_hose = hose;
  180. io7_port->csrs = csrs;
  181. io7_port->hose = hose;
  182. hose->sysdata = io7_port;
  183. hose->io_space = alloc_resource();
  184. hose->mem_space = alloc_resource();
  185. /*
  186. * Base addresses for userland consumption. Since these are going
  187. * to be mapped, they are pure physical addresses.
  188. */
  189. hose->sparse_mem_base = hose->sparse_io_base = 0;
  190. hose->dense_mem_base = IO7_MEM_PHYS(io7->pe, port);
  191. hose->dense_io_base = IO7_IO_PHYS(io7->pe, port);
  192. /*
  193. * Base addresses and resource ranges for kernel consumption.
  194. */
  195. hose->config_space_base = (unsigned long)IO7_CONF_KERN(io7->pe, port);
  196. hose->io_space->start = (unsigned long)IO7_IO_KERN(io7->pe, port);
  197. hose->io_space->end = hose->io_space->start + IO7_IO_SPACE - 1;
  198. hose->io_space->name = mk_resource_name(io7->pe, port, "IO");
  199. hose->io_space->flags = IORESOURCE_IO;
  200. hose->mem_space->start = (unsigned long)IO7_MEM_KERN(io7->pe, port);
  201. hose->mem_space->end = hose->mem_space->start + IO7_MEM_SPACE - 1;
  202. hose->mem_space->name = mk_resource_name(io7->pe, port, "MEM");
  203. hose->mem_space->flags = IORESOURCE_MEM;
  204. if (request_resource(&ioport_resource, hose->io_space) < 0)
  205. printk(KERN_ERR "Failed to request IO on hose %d\n",
  206. hose->index);
  207. if (request_resource(&iomem_resource, hose->mem_space) < 0)
  208. printk(KERN_ERR "Failed to request MEM on hose %d\n",
  209. hose->index);
  210. /*
  211. * Save the existing DMA window settings for later restoration.
  212. */
  213. for (i = 0; i < 4; i++) {
  214. io7_port->saved_wbase[i] = csrs->POx_WBASE[i].csr;
  215. io7_port->saved_wmask[i] = csrs->POx_WMASK[i].csr;
  216. io7_port->saved_tbase[i] = csrs->POx_TBASE[i].csr;
  217. }
  218. /*
  219. * Set up the PCI to main memory translation windows.
  220. *
  221. * Window 0 is scatter-gather 8MB at 8MB
  222. * Window 1 is direct access 1GB at 2GB
  223. * Window 2 is scatter-gather (up-to) 1GB at 3GB
  224. * Window 3 is disabled
  225. */
  226. /*
  227. * TBIA before modifying windows.
  228. */
  229. marvel_pci_tbi(hose, 0, -1);
  230. /*
  231. * Set up window 0 for scatter-gather 8MB at 8MB.
  232. */
  233. hose->sg_isa = iommu_arena_new_node(marvel_cpuid_to_nid(io7->pe),
  234. hose, 0x00800000, 0x00800000, 0);
  235. hose->sg_isa->align_entry = 8; /* cache line boundary */
  236. csrs->POx_WBASE[0].csr =
  237. hose->sg_isa->dma_base | wbase_m_ena | wbase_m_sg;
  238. csrs->POx_WMASK[0].csr = (hose->sg_isa->size - 1) & wbase_m_addr;
  239. csrs->POx_TBASE[0].csr = virt_to_phys(hose->sg_isa->ptes);
  240. /*
  241. * Set up window 1 for direct-mapped 1GB at 2GB.
  242. */
  243. csrs->POx_WBASE[1].csr = __direct_map_base | wbase_m_ena;
  244. csrs->POx_WMASK[1].csr = (__direct_map_size - 1) & wbase_m_addr;
  245. csrs->POx_TBASE[1].csr = 0;
  246. /*
  247. * Set up window 2 for scatter-gather (up-to) 1GB at 3GB.
  248. */
  249. hose->sg_pci = iommu_arena_new_node(marvel_cpuid_to_nid(io7->pe),
  250. hose, 0xc0000000, 0x40000000, 0);
  251. hose->sg_pci->align_entry = 8; /* cache line boundary */
  252. csrs->POx_WBASE[2].csr =
  253. hose->sg_pci->dma_base | wbase_m_ena | wbase_m_sg;
  254. csrs->POx_WMASK[2].csr = (hose->sg_pci->size - 1) & wbase_m_addr;
  255. csrs->POx_TBASE[2].csr = virt_to_phys(hose->sg_pci->ptes);
  256. /*
  257. * Disable window 3.
  258. */
  259. csrs->POx_WBASE[3].csr = 0;
  260. /*
  261. * Make sure that the AGP Monster Window is disabled.
  262. */
  263. csrs->POx_CTRL.csr &= ~(1UL << 61);
  264. #if 1
  265. printk("FIXME: disabling master aborts\n");
  266. csrs->POx_MSK_HEI.csr &= ~(3UL << 14);
  267. #endif
  268. /*
  269. * TBIA after modifying windows.
  270. */
  271. marvel_pci_tbi(hose, 0, -1);
  272. }
  273. static void __init
  274. marvel_init_io7(struct io7 *io7)
  275. {
  276. int i;
  277. printk("Initializing IO7 at PID %d\n", io7->pe);
  278. /*
  279. * Get the Port 7 CSR pointer.
  280. */
  281. io7->csrs = IO7_PORT7_CSRS_KERN(io7->pe);
  282. /*
  283. * Init this IO7's hoses.
  284. */
  285. for (i = 0; i < IO7_NUM_PORTS; i++) {
  286. io7_ioport_csrs *csrs = IO7_CSRS_KERN(io7->pe, i);
  287. if (csrs->POx_CACHE_CTL.csr == 8) {
  288. io7->ports[i].enabled = 1;
  289. io7_init_hose(io7, i);
  290. }
  291. }
  292. }
  293. void
  294. marvel_io7_present(gct6_node *node)
  295. {
  296. int pe;
  297. if (node->type != GCT_TYPE_HOSE ||
  298. node->subtype != GCT_SUBTYPE_IO_PORT_MODULE)
  299. return;
  300. pe = (node->id >> 8) & 0xff;
  301. printk("Found an IO7 at PID %d\n", pe);
  302. alloc_io7(pe);
  303. }
  304. static void __init
  305. marvel_find_console_vga_hose(void)
  306. {
  307. u64 *pu64 = (u64 *)((u64)hwrpb + hwrpb->ctbt_offset);
  308. if (pu64[7] == 3) { /* TERM_TYPE == graphics */
  309. struct pci_controller *hose = NULL;
  310. int h = (pu64[30] >> 24) & 0xff; /* TERM_OUT_LOC, hose # */
  311. struct io7 *io7;
  312. int pid, port;
  313. /* FIXME - encoding is going to have to change for Marvel
  314. * since hose will be able to overflow a byte...
  315. * need to fix this decode when the console
  316. * changes its encoding
  317. */
  318. printk("console graphics is on hose %d (console)\n", h);
  319. /*
  320. * The console's hose numbering is:
  321. *
  322. * hose<n:2>: PID
  323. * hose<1:0>: PORT
  324. *
  325. * We need to find the hose at that pid and port
  326. */
  327. pid = h >> 2;
  328. port = h & 3;
  329. if ((io7 = marvel_find_io7(pid)))
  330. hose = io7->ports[port].hose;
  331. if (hose) {
  332. printk("Console graphics on hose %d\n", hose->index);
  333. pci_vga_hose = hose;
  334. }
  335. }
  336. }
  337. gct6_search_struct gct_wanted_node_list[] = {
  338. { GCT_TYPE_HOSE, GCT_SUBTYPE_IO_PORT_MODULE, marvel_io7_present },
  339. { 0, 0, NULL }
  340. };
  341. /*
  342. * In case the GCT is not complete, let the user specify PIDs with IO7s
  343. * at boot time. Syntax is 'io7=a,b,c,...,n' where a-n are the PIDs (decimal)
  344. * where IO7s are connected
  345. */
  346. static int __init
  347. marvel_specify_io7(char *str)
  348. {
  349. unsigned long pid;
  350. struct io7 *io7;
  351. char *pchar;
  352. do {
  353. pid = simple_strtoul(str, &pchar, 0);
  354. if (pchar != str) {
  355. printk("User-specified IO7 at PID %lu\n", pid);
  356. io7 = alloc_io7(pid);
  357. if (io7) marvel_init_io7(io7);
  358. }
  359. if (pchar == str) pchar++;
  360. str = pchar;
  361. } while(*str);
  362. return 1;
  363. }
  364. __setup("io7=", marvel_specify_io7);
  365. void __init
  366. marvel_init_arch(void)
  367. {
  368. struct io7 *io7;
  369. /* With multiple PCI busses, we play with I/O as physical addrs. */
  370. ioport_resource.end = ~0UL;
  371. /* PCI DMA Direct Mapping is 1GB at 2GB. */
  372. __direct_map_base = 0x80000000;
  373. __direct_map_size = 0x40000000;
  374. /* Parse the config tree. */
  375. gct6_find_nodes(GCT_NODE_PTR(0), gct_wanted_node_list);
  376. /* Init the io7s. */
  377. for (io7 = NULL; NULL != (io7 = marvel_next_io7(io7)); )
  378. marvel_init_io7(io7);
  379. /* Check for graphic console location (if any). */
  380. marvel_find_console_vga_hose();
  381. }
  382. void
  383. marvel_kill_arch(int mode)
  384. {
  385. }
  386. /*
  387. * PCI Configuration Space access functions
  388. *
  389. * Configuration space addresses have the following format:
  390. *
  391. * |2 2 2 2|1 1 1 1|1 1 1 1|1 1
  392. * |3 2 1 0|9 8 7 6|5 4 3 2|1 0 9 8|7 6 5 4|3 2 1 0
  393. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  394. * |B|B|B|B|B|B|B|B|D|D|D|D|D|F|F|F|R|R|R|R|R|R|R|R|
  395. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  396. *
  397. * n:24 reserved for hose base
  398. * 23:16 bus number (8 bits = 128 possible buses)
  399. * 15:11 Device number (5 bits)
  400. * 10:8 function number
  401. * 7:2 register number
  402. *
  403. * Notes:
  404. * IO7 determines whether to use a type 0 or type 1 config cycle
  405. * based on the bus number. Therefore the bus number must be set
  406. * to 0 for the root bus on any hose.
  407. *
  408. * The function number selects which function of a multi-function device
  409. * (e.g., SCSI and Ethernet).
  410. *
  411. */
  412. static inline unsigned long
  413. build_conf_addr(struct pci_controller *hose, u8 bus,
  414. unsigned int devfn, int where)
  415. {
  416. return (hose->config_space_base | (bus << 16) | (devfn << 8) | where);
  417. }
  418. static unsigned long
  419. mk_conf_addr(struct pci_bus *pbus, unsigned int devfn, int where)
  420. {
  421. struct pci_controller *hose = pbus->sysdata;
  422. struct io7_port *io7_port;
  423. unsigned long addr = 0;
  424. u8 bus = pbus->number;
  425. if (!hose)
  426. return addr;
  427. /* Check for enabled. */
  428. io7_port = hose->sysdata;
  429. if (!io7_port->enabled)
  430. return addr;
  431. if (!pbus->parent) { /* No parent means peer PCI bus. */
  432. /* Don't support idsel > 20 on primary bus. */
  433. if (devfn >= PCI_DEVFN(21, 0))
  434. return addr;
  435. bus = 0;
  436. }
  437. addr = build_conf_addr(hose, bus, devfn, where);
  438. DBG_CFG(("mk_conf_addr: returning pci_addr 0x%lx\n", addr));
  439. return addr;
  440. }
  441. static int
  442. marvel_read_config(struct pci_bus *bus, unsigned int devfn, int where,
  443. int size, u32 *value)
  444. {
  445. unsigned long addr;
  446. if (0 == (addr = mk_conf_addr(bus, devfn, where)))
  447. return PCIBIOS_DEVICE_NOT_FOUND;
  448. switch(size) {
  449. case 1:
  450. *value = __kernel_ldbu(*(vucp)addr);
  451. break;
  452. case 2:
  453. *value = __kernel_ldwu(*(vusp)addr);
  454. break;
  455. case 4:
  456. *value = *(vuip)addr;
  457. break;
  458. default:
  459. return PCIBIOS_FUNC_NOT_SUPPORTED;
  460. }
  461. return PCIBIOS_SUCCESSFUL;
  462. }
  463. static int
  464. marvel_write_config(struct pci_bus *bus, unsigned int devfn, int where,
  465. int size, u32 value)
  466. {
  467. unsigned long addr;
  468. if (0 == (addr = mk_conf_addr(bus, devfn, where)))
  469. return PCIBIOS_DEVICE_NOT_FOUND;
  470. switch (size) {
  471. case 1:
  472. __kernel_stb(value, *(vucp)addr);
  473. mb();
  474. __kernel_ldbu(*(vucp)addr);
  475. break;
  476. case 2:
  477. __kernel_stw(value, *(vusp)addr);
  478. mb();
  479. __kernel_ldwu(*(vusp)addr);
  480. break;
  481. case 4:
  482. *(vuip)addr = value;
  483. mb();
  484. *(vuip)addr;
  485. break;
  486. default:
  487. return PCIBIOS_FUNC_NOT_SUPPORTED;
  488. }
  489. return PCIBIOS_SUCCESSFUL;
  490. }
  491. struct pci_ops marvel_pci_ops =
  492. {
  493. .read = marvel_read_config,
  494. .write = marvel_write_config,
  495. };
  496. /*
  497. * Other PCI helper functions.
  498. */
  499. void
  500. marvel_pci_tbi(struct pci_controller *hose, dma_addr_t start, dma_addr_t end)
  501. {
  502. io7_ioport_csrs *csrs = ((struct io7_port *)hose->sysdata)->csrs;
  503. wmb();
  504. csrs->POx_SG_TBIA.csr = 0;
  505. mb();
  506. csrs->POx_SG_TBIA.csr;
  507. }
  508. /*
  509. * RTC Support
  510. */
  511. struct marvel_rtc_access_info {
  512. unsigned long function;
  513. unsigned long index;
  514. unsigned long data;
  515. };
  516. static void
  517. __marvel_access_rtc(void *info)
  518. {
  519. struct marvel_rtc_access_info *rtc_access = info;
  520. register unsigned long __r0 __asm__("$0");
  521. register unsigned long __r16 __asm__("$16") = rtc_access->function;
  522. register unsigned long __r17 __asm__("$17") = rtc_access->index;
  523. register unsigned long __r18 __asm__("$18") = rtc_access->data;
  524. __asm__ __volatile__(
  525. "call_pal %4 # cserve rtc"
  526. : "=r"(__r16), "=r"(__r17), "=r"(__r18), "=r"(__r0)
  527. : "i"(PAL_cserve), "0"(__r16), "1"(__r17), "2"(__r18)
  528. : "$1", "$22", "$23", "$24", "$25");
  529. rtc_access->data = __r0;
  530. }
  531. static u8
  532. __marvel_rtc_io(u8 b, unsigned long addr, int write)
  533. {
  534. static u8 index = 0;
  535. struct marvel_rtc_access_info rtc_access;
  536. u8 ret = 0;
  537. switch(addr) {
  538. case 0x70: /* RTC_PORT(0) */
  539. if (write) index = b;
  540. ret = index;
  541. break;
  542. case 0x71: /* RTC_PORT(1) */
  543. rtc_access.index = index;
  544. rtc_access.data = BCD_TO_BIN(b);
  545. rtc_access.function = 0x48 + !write; /* GET/PUT_TOY */
  546. #ifdef CONFIG_SMP
  547. if (smp_processor_id() != boot_cpuid)
  548. smp_call_function_single(boot_cpuid,
  549. __marvel_access_rtc,
  550. &rtc_access, 1);
  551. else
  552. __marvel_access_rtc(&rtc_access);
  553. #else
  554. __marvel_access_rtc(&rtc_access);
  555. #endif
  556. ret = BIN_TO_BCD(rtc_access.data);
  557. break;
  558. default:
  559. printk(KERN_WARNING "Illegal RTC port %lx\n", addr);
  560. break;
  561. }
  562. return ret;
  563. }
  564. /*
  565. * IO map support.
  566. */
  567. void __iomem *
  568. marvel_ioremap(unsigned long addr, unsigned long size)
  569. {
  570. struct pci_controller *hose;
  571. unsigned long baddr, last;
  572. struct vm_struct *area;
  573. unsigned long vaddr;
  574. unsigned long *ptes;
  575. unsigned long pfn;
  576. /*
  577. * Adjust the address.
  578. */
  579. FIXUP_MEMADDR_VGA(addr);
  580. /*
  581. * Find the hose.
  582. */
  583. for (hose = hose_head; hose; hose = hose->next) {
  584. if ((addr >> 32) == (hose->mem_space->start >> 32))
  585. break;
  586. }
  587. if (!hose)
  588. return NULL;
  589. /*
  590. * We have the hose - calculate the bus limits.
  591. */
  592. baddr = addr - hose->mem_space->start;
  593. last = baddr + size - 1;
  594. /*
  595. * Is it direct-mapped?
  596. */
  597. if ((baddr >= __direct_map_base) &&
  598. ((baddr + size - 1) < __direct_map_base + __direct_map_size)) {
  599. addr = IDENT_ADDR | (baddr - __direct_map_base);
  600. return (void __iomem *) addr;
  601. }
  602. /*
  603. * Check the scatter-gather arena.
  604. */
  605. if (hose->sg_pci &&
  606. baddr >= (unsigned long)hose->sg_pci->dma_base &&
  607. last < (unsigned long)hose->sg_pci->dma_base + hose->sg_pci->size) {
  608. /*
  609. * Adjust the limits (mappings must be page aligned)
  610. */
  611. baddr -= hose->sg_pci->dma_base;
  612. last -= hose->sg_pci->dma_base;
  613. baddr &= PAGE_MASK;
  614. size = PAGE_ALIGN(last) - baddr;
  615. /*
  616. * Map it.
  617. */
  618. area = get_vm_area(size, VM_IOREMAP);
  619. if (!area)
  620. return NULL;
  621. ptes = hose->sg_pci->ptes;
  622. for (vaddr = (unsigned long)area->addr;
  623. baddr <= last;
  624. baddr += PAGE_SIZE, vaddr += PAGE_SIZE) {
  625. pfn = ptes[baddr >> PAGE_SHIFT];
  626. if (!(pfn & 1)) {
  627. printk("ioremap failed... pte not valid...\n");
  628. vfree(area->addr);
  629. return NULL;
  630. }
  631. pfn >>= 1; /* make it a true pfn */
  632. if (__alpha_remap_area_pages(vaddr,
  633. pfn << PAGE_SHIFT,
  634. PAGE_SIZE, 0)) {
  635. printk("FAILED to map...\n");
  636. vfree(area->addr);
  637. return NULL;
  638. }
  639. }
  640. flush_tlb_all();
  641. vaddr = (unsigned long)area->addr + (addr & ~PAGE_MASK);
  642. return (void __iomem *) vaddr;
  643. }
  644. /* Assume it was already a reasonable address */
  645. vaddr = baddr + hose->mem_space->start;
  646. return (void __iomem *) vaddr;
  647. }
  648. void
  649. marvel_iounmap(volatile void __iomem *xaddr)
  650. {
  651. unsigned long addr = (unsigned long) xaddr;
  652. if (addr >= VMALLOC_START)
  653. vfree((void *)(PAGE_MASK & addr));
  654. }
  655. int
  656. marvel_is_mmio(const volatile void __iomem *xaddr)
  657. {
  658. unsigned long addr = (unsigned long) xaddr;
  659. if (addr >= VMALLOC_START)
  660. return 1;
  661. else
  662. return (addr & 0xFF000000UL) == 0;
  663. }
  664. #define __marvel_is_port_kbd(a) (((a) == 0x60) || ((a) == 0x64))
  665. #define __marvel_is_port_rtc(a) (((a) == 0x70) || ((a) == 0x71))
  666. void __iomem *marvel_ioportmap (unsigned long addr)
  667. {
  668. FIXUP_IOADDR_VGA(addr);
  669. return (void __iomem *)addr;
  670. }
  671. unsigned int
  672. marvel_ioread8(void __iomem *xaddr)
  673. {
  674. unsigned long addr = (unsigned long) xaddr;
  675. if (__marvel_is_port_kbd(addr))
  676. return 0;
  677. else if (__marvel_is_port_rtc(addr))
  678. return __marvel_rtc_io(0, addr, 0);
  679. else if (marvel_is_ioaddr(addr))
  680. return __kernel_ldbu(*(vucp)addr);
  681. else
  682. /* this should catch other legacy addresses
  683. that would normally fail on MARVEL,
  684. because there really is nothing there...
  685. */
  686. return ~0;
  687. }
  688. void
  689. marvel_iowrite8(u8 b, void __iomem *xaddr)
  690. {
  691. unsigned long addr = (unsigned long) xaddr;
  692. if (__marvel_is_port_kbd(addr))
  693. return;
  694. else if (__marvel_is_port_rtc(addr))
  695. __marvel_rtc_io(b, addr, 1);
  696. else if (marvel_is_ioaddr(addr))
  697. __kernel_stb(b, *(vucp)addr);
  698. }
  699. #ifndef CONFIG_ALPHA_GENERIC
  700. EXPORT_SYMBOL(marvel_ioremap);
  701. EXPORT_SYMBOL(marvel_iounmap);
  702. EXPORT_SYMBOL(marvel_is_mmio);
  703. EXPORT_SYMBOL(marvel_ioportmap);
  704. EXPORT_SYMBOL(marvel_ioread8);
  705. EXPORT_SYMBOL(marvel_iowrite8);
  706. #endif
  707. /*
  708. * NUMA Support
  709. */
  710. /**********
  711. * FIXME - for now each cpu is a node by itself
  712. * -- no real support for striped mode
  713. **********
  714. */
  715. int
  716. marvel_pa_to_nid(unsigned long pa)
  717. {
  718. int cpuid;
  719. if ((pa >> 43) & 1) /* I/O */
  720. cpuid = (~(pa >> 35) & 0xff);
  721. else /* mem */
  722. cpuid = ((pa >> 34) & 0x3) | ((pa >> (37 - 2)) & (0x1f << 2));
  723. return marvel_cpuid_to_nid(cpuid);
  724. }
  725. int
  726. marvel_cpuid_to_nid(int cpuid)
  727. {
  728. return cpuid;
  729. }
  730. unsigned long
  731. marvel_node_mem_start(int nid)
  732. {
  733. unsigned long pa;
  734. pa = (nid & 0x3) | ((nid & (0x1f << 2)) << 1);
  735. pa <<= 34;
  736. return pa;
  737. }
  738. unsigned long
  739. marvel_node_mem_size(int nid)
  740. {
  741. return 16UL * 1024 * 1024 * 1024; /* 16GB */
  742. }
  743. /*
  744. * AGP GART Support.
  745. */
  746. #include <linux/agp_backend.h>
  747. #include <asm/agp_backend.h>
  748. #include <linux/slab.h>
  749. #include <linux/delay.h>
  750. struct marvel_agp_aperture {
  751. struct pci_iommu_arena *arena;
  752. long pg_start;
  753. long pg_count;
  754. };
  755. static int
  756. marvel_agp_setup(alpha_agp_info *agp)
  757. {
  758. struct marvel_agp_aperture *aper;
  759. if (!alpha_agpgart_size)
  760. return -ENOMEM;
  761. aper = kmalloc(sizeof(*aper), GFP_KERNEL);
  762. if (aper == NULL) return -ENOMEM;
  763. aper->arena = agp->hose->sg_pci;
  764. aper->pg_count = alpha_agpgart_size / PAGE_SIZE;
  765. aper->pg_start = iommu_reserve(aper->arena, aper->pg_count,
  766. aper->pg_count - 1);
  767. if (aper->pg_start < 0) {
  768. printk(KERN_ERR "Failed to reserve AGP memory\n");
  769. kfree(aper);
  770. return -ENOMEM;
  771. }
  772. agp->aperture.bus_base =
  773. aper->arena->dma_base + aper->pg_start * PAGE_SIZE;
  774. agp->aperture.size = aper->pg_count * PAGE_SIZE;
  775. agp->aperture.sysdata = aper;
  776. return 0;
  777. }
  778. static void
  779. marvel_agp_cleanup(alpha_agp_info *agp)
  780. {
  781. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  782. int status;
  783. status = iommu_release(aper->arena, aper->pg_start, aper->pg_count);
  784. if (status == -EBUSY) {
  785. printk(KERN_WARNING
  786. "Attempted to release bound AGP memory - unbinding\n");
  787. iommu_unbind(aper->arena, aper->pg_start, aper->pg_count);
  788. status = iommu_release(aper->arena, aper->pg_start,
  789. aper->pg_count);
  790. }
  791. if (status < 0)
  792. printk(KERN_ERR "Failed to release AGP memory\n");
  793. kfree(aper);
  794. kfree(agp);
  795. }
  796. static int
  797. marvel_agp_configure(alpha_agp_info *agp)
  798. {
  799. io7_ioport_csrs *csrs = ((struct io7_port *)agp->hose->sysdata)->csrs;
  800. struct io7 *io7 = ((struct io7_port *)agp->hose->sysdata)->io7;
  801. unsigned int new_rate = 0;
  802. unsigned long agp_pll;
  803. /*
  804. * Check the requested mode against the PLL setting.
  805. * The agpgart_be code has not programmed the card yet,
  806. * so we can still tweak mode here.
  807. */
  808. agp_pll = io7->csrs->POx_RST[IO7_AGP_PORT].csr;
  809. switch(IO7_PLL_RNGB(agp_pll)) {
  810. case 0x4: /* 2x only */
  811. /*
  812. * The PLL is only programmed for 2x, so adjust the
  813. * rate to 2x, if necessary.
  814. */
  815. if (agp->mode.bits.rate != 2)
  816. new_rate = 2;
  817. break;
  818. case 0x6: /* 1x / 4x */
  819. /*
  820. * The PLL is programmed for 1x or 4x. Don't go faster
  821. * than requested, so if the requested rate is 2x, use 1x.
  822. */
  823. if (agp->mode.bits.rate == 2)
  824. new_rate = 1;
  825. break;
  826. default: /* ??????? */
  827. /*
  828. * Don't know what this PLL setting is, take the requested
  829. * rate, but warn the user.
  830. */
  831. printk("%s: unknown PLL setting RNGB=%lx (PLL6_CTL=%016lx)\n",
  832. __func__, IO7_PLL_RNGB(agp_pll), agp_pll);
  833. break;
  834. }
  835. /*
  836. * Set the new rate, if necessary.
  837. */
  838. if (new_rate) {
  839. printk("Requested AGP Rate %dX not compatible "
  840. "with PLL setting - using %dX\n",
  841. agp->mode.bits.rate,
  842. new_rate);
  843. agp->mode.bits.rate = new_rate;
  844. }
  845. printk("Enabling AGP on hose %d: %dX%s RQ %d\n",
  846. agp->hose->index, agp->mode.bits.rate,
  847. agp->mode.bits.sba ? " - SBA" : "", agp->mode.bits.rq);
  848. csrs->AGP_CMD.csr = agp->mode.lw;
  849. return 0;
  850. }
  851. static int
  852. marvel_agp_bind_memory(alpha_agp_info *agp, off_t pg_start, struct agp_memory *mem)
  853. {
  854. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  855. return iommu_bind(aper->arena, aper->pg_start + pg_start,
  856. mem->page_count, mem->memory);
  857. }
  858. static int
  859. marvel_agp_unbind_memory(alpha_agp_info *agp, off_t pg_start, struct agp_memory *mem)
  860. {
  861. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  862. return iommu_unbind(aper->arena, aper->pg_start + pg_start,
  863. mem->page_count);
  864. }
  865. static unsigned long
  866. marvel_agp_translate(alpha_agp_info *agp, dma_addr_t addr)
  867. {
  868. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  869. unsigned long baddr = addr - aper->arena->dma_base;
  870. unsigned long pte;
  871. if (addr < agp->aperture.bus_base ||
  872. addr >= agp->aperture.bus_base + agp->aperture.size) {
  873. printk("%s: addr out of range\n", __func__);
  874. return -EINVAL;
  875. }
  876. pte = aper->arena->ptes[baddr >> PAGE_SHIFT];
  877. if (!(pte & 1)) {
  878. printk("%s: pte not valid\n", __func__);
  879. return -EINVAL;
  880. }
  881. return (pte >> 1) << PAGE_SHIFT;
  882. }
  883. struct alpha_agp_ops marvel_agp_ops =
  884. {
  885. .setup = marvel_agp_setup,
  886. .cleanup = marvel_agp_cleanup,
  887. .configure = marvel_agp_configure,
  888. .bind = marvel_agp_bind_memory,
  889. .unbind = marvel_agp_unbind_memory,
  890. .translate = marvel_agp_translate
  891. };
  892. alpha_agp_info *
  893. marvel_agp_info(void)
  894. {
  895. struct pci_controller *hose;
  896. io7_ioport_csrs *csrs;
  897. alpha_agp_info *agp;
  898. struct io7 *io7;
  899. /*
  900. * Find the first IO7 with an AGP card.
  901. *
  902. * FIXME -- there should be a better way (we want to be able to
  903. * specify and what if the agp card is not video???)
  904. */
  905. hose = NULL;
  906. for (io7 = NULL; (io7 = marvel_next_io7(io7)) != NULL; ) {
  907. struct pci_controller *h;
  908. vuip addr;
  909. if (!io7->ports[IO7_AGP_PORT].enabled)
  910. continue;
  911. h = io7->ports[IO7_AGP_PORT].hose;
  912. addr = (vuip)build_conf_addr(h, 0, PCI_DEVFN(5, 0), 0);
  913. if (*addr != 0xffffffffu) {
  914. hose = h;
  915. break;
  916. }
  917. }
  918. if (!hose || !hose->sg_pci)
  919. return NULL;
  920. printk("MARVEL - using hose %d as AGP\n", hose->index);
  921. /*
  922. * Get the csrs from the hose.
  923. */
  924. csrs = ((struct io7_port *)hose->sysdata)->csrs;
  925. /*
  926. * Allocate the info structure.
  927. */
  928. agp = kmalloc(sizeof(*agp), GFP_KERNEL);
  929. /*
  930. * Fill it in.
  931. */
  932. agp->hose = hose;
  933. agp->private = NULL;
  934. agp->ops = &marvel_agp_ops;
  935. /*
  936. * Aperture - not configured until ops.setup().
  937. */
  938. agp->aperture.bus_base = 0;
  939. agp->aperture.size = 0;
  940. agp->aperture.sysdata = NULL;
  941. /*
  942. * Capabilities.
  943. *
  944. * NOTE: IO7 reports through AGP_STAT that it can support a read queue
  945. * depth of 17 (rq = 0x10). It actually only supports a depth of
  946. * 16 (rq = 0xf).
  947. */
  948. agp->capability.lw = csrs->AGP_STAT.csr;
  949. agp->capability.bits.rq = 0xf;
  950. /*
  951. * Mode.
  952. */
  953. agp->mode.lw = csrs->AGP_CMD.csr;
  954. return agp;
  955. }