// SPDX-License-Identifier: GPL-2.0-or-later1/*2* init_ohci1394_dma.c - Initializes physical DMA on all OHCI 1394 controllers3*4* Copyright (C) 2006-2007 Bernhard Kaindl <[email protected]>5*6* Derived from drivers/ieee1394/ohci1394.c and arch/x86/kernel/early-quirks.c7* this file has functions to:8* - scan the PCI very early on boot for all OHCI 1394-compliant controllers9* - reset and initialize them and make them join the IEEE1394 bus and10* - enable physical DMA on them to allow remote debugging11*12* All code and data is marked as __init and __initdata, respective as13* during boot, all OHCI1394 controllers may be claimed by the firewire14* stack and at this point, this code should not touch them anymore.15*16* To use physical DMA after the initialization of the firewire stack,17* be sure that the stack enables it and (re-)attach after the bus reset18* which may be caused by the firewire stack initialization.19*/2021#include <linux/delay.h>22#include <linux/io.h>23#include <linux/kernel.h>24#include <linux/pci.h> /* for PCI defines */25#include <linux/string.h>2627#include <asm/pci-direct.h> /* for direct PCI config space access */28#include <asm/fixmap.h>2930#include <linux/init_ohci1394_dma.h>31#include "ohci.h"3233int __initdata init_ohci1394_dma_early;3435struct ohci {36void __iomem *registers;37};3839static inline void reg_write(const struct ohci *ohci, int offset, u32 data)40{41writel(data, ohci->registers + offset);42}4344static inline u32 reg_read(const struct ohci *ohci, int offset)45{46return readl(ohci->registers + offset);47}4849#define OHCI_LOOP_COUNT 100 /* Number of loops for reg read waits */5051/* Reads a PHY register of an OHCI-1394 controller */52static inline u8 __init get_phy_reg(struct ohci *ohci, u8 addr)53{54int i;55u32 r;5657reg_write(ohci, OHCI1394_PhyControl, (addr << 8) | 0x00008000);5859for (i = 0; i < OHCI_LOOP_COUNT; i++) {60if (reg_read(ohci, OHCI1394_PhyControl) & 0x80000000)61break;62mdelay(1);63}64r = reg_read(ohci, OHCI1394_PhyControl);6566return (r & 0x00ff0000) >> 16;67}6869/* Writes to a PHY register of an OHCI-1394 controller */70static inline void __init set_phy_reg(struct ohci *ohci, u8 addr, u8 data)71{72int i;7374reg_write(ohci, OHCI1394_PhyControl, (addr << 8) | data | 0x00004000);7576for (i = 0; i < OHCI_LOOP_COUNT; i++) {77if (!(reg_read(ohci, OHCI1394_PhyControl) & 0x00004000))78break;79mdelay(1);80}81}8283/* Resets an OHCI-1394 controller (for sane state before initialization) */84static inline void __init init_ohci1394_soft_reset(struct ohci *ohci)85{86int i;8788reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);8990for (i = 0; i < OHCI_LOOP_COUNT; i++) {91if (!(reg_read(ohci, OHCI1394_HCControlSet)92& OHCI1394_HCControl_softReset))93break;94mdelay(1);95}96}9798#define OHCI1394_MAX_AT_REQ_RETRIES 0xf99#define OHCI1394_MAX_AT_RESP_RETRIES 0x2100#define OHCI1394_MAX_PHYS_RESP_RETRIES 0x8101102/* Basic OHCI-1394 register and port inititalization */103static inline void __init init_ohci1394_initialize(struct ohci *ohci)104{105u32 bus_options;106int num_ports, i;107108/* Put some defaults to these undefined bus options */109bus_options = reg_read(ohci, OHCI1394_BusOptions);110bus_options |= 0x60000000; /* Enable CMC and ISC */111bus_options &= ~0x00ff0000; /* XXX: Set cyc_clk_acc to zero for now */112bus_options &= ~0x18000000; /* Disable PMC and BMC */113reg_write(ohci, OHCI1394_BusOptions, bus_options);114115/* Set the bus number */116reg_write(ohci, OHCI1394_NodeID, 0x0000ffc0);117118/* Enable posted writes */119reg_write(ohci, OHCI1394_HCControlSet,120OHCI1394_HCControl_postedWriteEnable);121122/* Clear link control register */123reg_write(ohci, OHCI1394_LinkControlClear, 0xffffffff);124125/* enable phys */126reg_write(ohci, OHCI1394_LinkControlSet,127OHCI1394_LinkControl_rcvPhyPkt);128129/* Don't accept phy packets into AR request context */130reg_write(ohci, OHCI1394_LinkControlClear, 0x00000400);131132/* Clear the Isochonouys interrupt masks */133reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 0xffffffff);134reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 0xffffffff);135reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 0xffffffff);136reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 0xffffffff);137138/* Accept asynchronous transfer requests from all nodes for now */139reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);140141/* Specify asynchronous transfer retries */142reg_write(ohci, OHCI1394_ATRetries,143OHCI1394_MAX_AT_REQ_RETRIES |144(OHCI1394_MAX_AT_RESP_RETRIES<<4) |145(OHCI1394_MAX_PHYS_RESP_RETRIES<<8));146147/* We don't want hardware swapping */148reg_write(ohci, OHCI1394_HCControlClear,149OHCI1394_HCControl_noByteSwapData);150151/* Enable link */152reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_linkEnable);153154/* If anything is connected to a port, make sure it is enabled */155num_ports = get_phy_reg(ohci, 2) & 0xf;156for (i = 0; i < num_ports; i++) {157unsigned int status;158159set_phy_reg(ohci, 7, i);160status = get_phy_reg(ohci, 8);161162if (status & 0x20)163set_phy_reg(ohci, 8, status & ~1);164}165}166167/**168* init_ohci1394_wait_for_busresets - wait until bus resets are completed169* @ohci: Pointer to the OHCI-1394 controller structure170*171* OHCI1394 initialization itself and any device going on- or offline172* and any cable issue cause a IEEE1394 bus reset. The OHCI1394 spec173* specifies that physical DMA is disabled on each bus reset and it174* has to be enabled after each bus reset when needed. We resort175* to polling here because on early boot, we have no interrupts.176*/177static inline void __init init_ohci1394_wait_for_busresets(struct ohci *ohci)178{179int i, events;180181for (i = 0; i < 9; i++) {182mdelay(200);183events = reg_read(ohci, OHCI1394_IntEventSet);184if (events & OHCI1394_busReset)185reg_write(ohci, OHCI1394_IntEventClear,186OHCI1394_busReset);187}188}189190/**191* init_ohci1394_enable_physical_dma - Enable physical DMA for remote debugging192* @ohci: Pointer to the OHCI-1394 controller structure193*194* This enables remote DMA access over IEEE1394 from every host for the low195* 4GB of address space. DMA accesses above 4GB are not available currently.196*/197static inline void __init init_ohci1394_enable_physical_dma(struct ohci *ohci)198{199reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 0xffffffff);200reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 0xffffffff);201reg_write(ohci, OHCI1394_PhyUpperBound, 0xffff0000);202}203204/**205* init_ohci1394_reset_and_init_dma - init controller and enable DMA206* @ohci: Pointer to the OHCI-1394 controller structure207*208* This initializes the given controller and enables physical DMA engine in it.209*/210static inline void __init init_ohci1394_reset_and_init_dma(struct ohci *ohci)211{212/* Start off with a soft reset, clears everything to a sane state. */213init_ohci1394_soft_reset(ohci);214215/* Accessing some registers without LPS enabled may cause lock up */216reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_LPS);217218/* Disable and clear interrupts */219reg_write(ohci, OHCI1394_IntEventClear, 0xffffffff);220reg_write(ohci, OHCI1394_IntMaskClear, 0xffffffff);221222mdelay(50); /* Wait 50msec to make sure we have full link enabled */223224init_ohci1394_initialize(ohci);225/*226* The initialization causes at least one IEEE1394 bus reset. Enabling227* physical DMA only works *after* *all* bus resets have calmed down:228*/229init_ohci1394_wait_for_busresets(ohci);230231/* We had to wait and do this now if we want to debug early problems */232init_ohci1394_enable_physical_dma(ohci);233}234235/**236* init_ohci1394_controller - Map the registers of the controller and init DMA237* @num: PCI bus number238* @slot: PCI device number239* @func: PCI function number240*241* This maps the registers of the specified controller and initializes it242*/243static inline void __init init_ohci1394_controller(int num, int slot, int func)244{245unsigned long ohci_base;246struct ohci ohci;247248printk(KERN_INFO "init_ohci1394_dma: initializing OHCI-1394"249" at %02x:%02x.%x\n", num, slot, func);250251ohci_base = read_pci_config(num, slot, func, PCI_BASE_ADDRESS_0+(0<<2))252& PCI_BASE_ADDRESS_MEM_MASK;253254set_fixmap_nocache(FIX_OHCI1394_BASE, ohci_base);255256ohci.registers = (void __iomem *)fix_to_virt(FIX_OHCI1394_BASE);257258init_ohci1394_reset_and_init_dma(&ohci);259}260261/**262* init_ohci1394_dma_on_all_controllers - scan for OHCI1394 controllers and init DMA on them263* Scans the whole PCI space for OHCI1394 controllers and inits DMA on them264*/265void __init init_ohci1394_dma_on_all_controllers(void)266{267int num, slot, func;268u32 class;269270if (!early_pci_allowed())271return;272273/* Poor man's PCI discovery, the only thing we can do at early boot */274for (num = 0; num < 32; num++) {275for (slot = 0; slot < 32; slot++) {276for (func = 0; func < 8; func++) {277class = read_pci_config(num, slot, func,278PCI_CLASS_REVISION);279if (class == 0xffffffff)280continue; /* No device at this func */281282if (class>>8 != PCI_CLASS_SERIAL_FIREWIRE_OHCI)283continue; /* Not an OHCI-1394 device */284285init_ohci1394_controller(num, slot, func);286break; /* Assume one controller per device */287}288}289}290printk(KERN_INFO "init_ohci1394_dma: finished initializing OHCI DMA\n");291}292293/**294* setup_ohci1394_dma - enables early OHCI1394 DMA initialization295* @opt: Kernel command line parameter string296*/297static int __init setup_ohci1394_dma(char *opt)298{299if (!strcmp(opt, "early"))300init_ohci1394_dma_early = 1;301return 0;302}303304/* passing ohci1394_dma=early on boot causes early OHCI1394 DMA initialization */305early_param("ohci1394_dma", setup_ohci1394_dma);306307308