Skip to content

Add SMI driver - #1105

Closed
Wren6991 wants to merge 2 commits into
raspberrypi:rpi-4.1.yfrom
Wren6991:rpi-4.1.y
Closed

Wren6991 wants to merge 2 commits into
raspberrypi:rpi-4.1.yfrom
Wren6991:rpi-4.1.y

Conversation

@Wren6991

Copy link
Copy Markdown
Contributor

Secondary memory interface is a peripheral on the BCM2835 for talking to devices like parallel-interface LCDs and NAND flash chips.

This commit adds a driver for controlling SMI which registers a simple chardev + ioctl interface, and an example NAND driver using the functionality exposed by the SMI driver on the kernel side. It's the intention that we'll eventually be able to boot off of NAND flash, but this needs work on the firmware side.

It's very much a work in progress - any suggestions are appreciated!

@P33M

P33M commented Aug 18, 2015

Copy link
Copy Markdown
Contributor

It appears that this PR is still your development branch - the last commit has things in that are clearly still debug.

Usually the flow for adding a new feature has a sequence of commits that

  1. Add the base files (in this case the SMI driver itself plus headers) and Kconfig options for it
  2. Add the NAND driver and Kconfig options for it
  3. Switch on Kconfig/filesystem options (incl. e.g. jffs2/mtd) and add the overlay
    This splits out the code changes from the functional changes, useful when regression testing.

Also, please follow the kernel style guide (mainly for comments, but there's a few other places that are outside the coding style) https://www.kernel.org/doc/Documentation/CodingStyle and run the commits through checkpatch.pl.

I recommend creating a separate branch for commits that end up in a PR - avoids polluting the log with stuff that's still being worked on.

@Wren6991

Copy link
Copy Markdown
Contributor Author

Thanks, that sounds like very sound advice.
I'll make sure it passes cleanly through checkpatch.pl before making any other changes.

One question - when I checkout changes from my dev branch and push them to this PR, should I just squash the previous PR commits with new ones? I was intending to squash the PR when @popcornmix is happy to merge and leave history on here for now so people can talk about commits, but is it better to keep the PR clean?

Thanks,
Luke

@popcornmix

Copy link
Copy Markdown
Collaborator

You've added a few =y config options. I'd like to know the change in size of kernel.img and free memory after boot (free -h) before and after this change. I worry that JFFS2 may bring in quite a lot of code.

Obviously =m on these would be preferable for users who don't use this, but may make booting from nand harder (although not impossible with initramfs).

@pelwell

pelwell commented Aug 18, 2015

Copy link
Copy Markdown
Contributor

I'd say keep the commits separate until they are approved - the user always has the option to view the changed files instead of the commits, which gives the same appearance as squashing, and this way you get to choose.

@Wren6991

Copy link
Copy Markdown
Contributor Author

@popcornmix

Without SMI: (SMI, MTD, JFFS2 = n)

$ free -h > without-smi && ls -l /boot/kernel7.img >> without-smi
             total       used       free     shared    buffers     cached
Mem:          925M        64M       861M         0B       9.9M        25M
-/+ buffers/cache:        28M       897M
Swap:          99M         0B        99M
-rwxr-xr-x 1 root root 4031788 Aug 18  2015 /boot/kernel7.img

With SMI (SMI, MTD, JFFS2 = y):

             total       used       free     shared    buffers     cached
Mem:          925M        61M       864M         0B       9.6M        23M
-/+ buffers/cache:        28M       897M
Swap:          99M         0B        99M
-rwxr-xr-x 1 root root 4146292 Aug 18  2015 /boot/kernel7.img

Adds about 100k to the kernel so pretty heavy when you include things like JFFS2.

@P33M

P33M commented Aug 18, 2015

Copy link
Copy Markdown
Contributor

That's the size of the compressed image.

A more useful comparison would be the reported memory map on boot from dmesg:

[    0.000000] Memory: 436600K/458752K available (6063K kernel code, 333K rwdata, 1908K rodata, 352K init, 720K bss, 13960K reserved, 8192K cma-reserved)
[    0.000000] Virtual kernel memory layout:
[    0.000000]     vector  : 0xffff0000 - 0xffff1000   (   4 kB)
[    0.000000]     fixmap  : 0xffc00000 - 0xfff00000   (3072 kB)
[    0.000000]     vmalloc : 0xdc800000 - 0xff000000   ( 552 MB)
[    0.000000]     lowmem  : 0xc0000000 - 0xdc000000   ( 448 MB)
[    0.000000]     modules : 0xbf000000 - 0xc0000000   (  16 MB)
[    0.000000]       .text : 0xc0008000 - 0xc07d10d0   (7973 kB)
[    0.000000]       .init : 0xc07d2000 - 0xc082a000   ( 352 kB)
[    0.000000]       .data : 0xc082a000 - 0xc087d674   ( 334 kB)
[    0.000000]        .bss : 0xc087d674 - 0xc0931778   ( 721 kB)

@Wren6991

Copy link
Copy Markdown
Contributor Author

Dom asked for the size of kernel.img, here's the layout from the top of dmesg:

Aug 18 14:17:10 raspberrypi kernel: [    0.000000] Memory: 939272K/966656K available (6128K kernel cod
e, 550K rwdata, 1692K rodata, 424K init, 760K bss, 19192K reserved, 8192K cma-reserved)
Aug 18 14:17:10 raspberrypi kernel: [    0.000000] Virtual kernel memory layout:
Aug 18 14:17:10 raspberrypi kernel: [    0.000000]     vector  : 0xffff0000 - 0xffff1000   (   4 kB)
Aug 18 14:17:10 raspberrypi kernel: [    0.000000]     fixmap  : 0xffc00000 - 0xfff00000   (3072 kB)
Aug 18 14:17:10 raspberrypi kernel: [    0.000000]     vmalloc : 0xbb800000 - 0xff000000   (1080 MB)
Aug 18 14:17:10 raspberrypi kernel: [    0.000000]     lowmem  : 0x80000000 - 0xbb000000   ( 944 MB)
Aug 18 14:17:10 raspberrypi kernel: [    0.000000]     modules : 0x7f000000 - 0x80000000   (  16 MB)
Aug 18 14:17:10 raspberrypi kernel: [    0.000000]       .text : 0x80008000 - 0x807ab41c   (7822 kB)
Aug 18 14:17:10 raspberrypi kernel: [    0.000000]       .init : 0x807ac000 - 0x80816000   ( 424 kB)
Aug 18 14:17:10 raspberrypi kernel: [    0.000000]       .data : 0x80816000 - 0x8089fa34   ( 551 kB)
Aug 18 14:17:10 raspberrypi kernel: [    0.000000]        .bss : 0x808a2000 - 0x8096030c   ( 761 kB)

@ghollingworth

Copy link
Copy Markdown

Luke,

It would be better to split this up into two commits, the first would be the SMI driver alone with nothing else. The second would be the flash + MTD interface implementation but without it enabled in the kernel by default.

This would avoid increasing the size of the kernel with jffs / MTD but would enable someone to add the functionality themselves if required (an app note for how to get this working would be good)

Gordon

@Wren6991

Copy link
Copy Markdown
Contributor Author

Hi Gordon,
When you say "not enabled in the kernel" do you mean enabled as a module rather than a builtin, or not enabled at all?

@notro

notro commented Aug 20, 2015

Copy link
Copy Markdown
Contributor

Is SMI really a 8080/6800 compatible bus interface? Maximum 18-bit databus and 5-bit address bus?

If this driver is heading for mainline, you can't have a global variable to hold the smi instance.
Even though there's only one smi hw device, the driver should be made to handle multiple devices.
So in the nand driver, bcm2835_smi_register() could return the instance which would be passed to the various smi functions.
drivers/firmware/raspberrypi.c is an example of this. rpi_firmware_get() returns the instance.
A DT phandle in the nand node should point to the smi node, and this will be used to get the smi instance.

I also suggest to split out the userpace interface into a module of it's own, like spidev and i2c-dev for the spi/i2c busses.
Then this module becomes a regular smi client like the nand driver is.

Also avoid making your own log functions like log_error(). Try and use dev_{err,info}() where possible. If you need pr_error() somewhere and want to prepend a string, you can use pr_fmt().

@P33M

P33M commented Aug 20, 2015

Copy link
Copy Markdown
Contributor

SMI is 8080/6800 compatible. There are 18 data lines (bits 16 and 17 can be repurposed as DREQ/DACK for paced DMA) and 6 address lines.

@notro

notro commented Aug 20, 2015

Copy link
Copy Markdown
Contributor

I just realised that drivers/firmware/raspberrypi.c is not a good example as it is currently, because it was hacked to work without Device Tree and that was done with a global variable. So look at the original version.

@Wren6991

Copy link
Copy Markdown
Contributor Author

Hi @notro, thanks very much for the input, that all sounds like good advice. I'll make those changes.

@Wren6991

Copy link
Copy Markdown
Contributor Author

@notro is this what you meant? Each client has a phandle for the SMI node in its own node, and calls bcm2835_smi_register() to get the instance pointer to pass to the SMI functions. (This commit)

@Wren6991

Copy link
Copy Markdown
Contributor Author

How do people feel about this now? There are still some things to clean up but it seems to be in a working state, I've done a certain amount of testing and someone else is using this internally for another driver.

Comment thread drivers/misc/bcm2835_smi.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cm_reg macro looks very convoluted.
This is a common pattern:

static inline void smi_write_reg(struct bcm2835_smi_instance *inst, unsigned reg, u32 val)
{
  writel(val, inst->smi_regs + reg);
}

static inline u32 smi_read_reg(struct bcm2835_smi_instance *inst, unsigned reg)
{
  return readl(inst->smi_regs + reg);
}

And similar for the cm_smi_regs.

@Wren6991

Copy link
Copy Markdown
Contributor Author

Thanks for that, @notro. I will change those, I think all of your points were good ones.
What is a good way to deal with concurrency issues if I start giving out the instance pointer to multiple drivers at a time? Is a big transaction mutex the way to go, or are there better ways?

@P33M

P33M commented Aug 27, 2015

Copy link
Copy Markdown
Contributor

For now, I'd just go with a big transaction mutex. This can be made more granular (direct vs FIFO/DMA transfers) as the SMI peripheral supports interleaving these two types but requires a bit of work to ensure that you can't end up with deadlocks.

@Wren6991

Copy link
Copy Markdown
Contributor Author

Okay, that sounds like the pragmatic thing to do.

@Wren6991

Copy link
Copy Markdown
Contributor Author

Had to use a lock rather than a mutex because some clients (JFFS2 + MTD) call into SMI from an atomic context.

@Wren6991

Copy link
Copy Markdown
Contributor Author

This seems to be working for me on Pi 1 and Pi 2. I'll ask Edd to start using this version of the driver and see if he runs into any problems.

@pelwell @popcornmix any thoughts on the current state?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't recall having seen status mentioned in a binding document before (and it is optional). You can remove it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I was just following the example of another binding document, maybe something Atmel related. I've removed that from the bindings.

@notro

notro commented Aug 29, 2015

Copy link
Copy Markdown
Contributor

The kernel doesn't have a subsystem for 8080 like busses, so I did some work on that some months ago: https://github.com/notro/fbdbi/tree/master/i80
This was done while looking at ways to rework the io path for drivers/staging/fbtft.
I plan to return to this at some point and see if I can get it into mainline in some form (adding scatter-gather among other things).

So I wonder, can smi be used for anything else than as a 6800/8080 bus interface?

Comment thread include/linux/broadcom/bcm2835_smi.h Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to just store the scatterlist structure here, instead of using pointers and allocate later?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason I was under the impression they had to be page-aligned, not sure if this is the case. I'll check.

@pelwell

pelwell commented Sep 3, 2015

Copy link
Copy Markdown
Contributor

@notro has given lots of great feedback, much of which has been incorporated, and now we are down to more-or-less cosmetic/taste issues. I can live with the drivers as they are, and I'm keen to get them merged in so others can start to use them.

@Wren6991 Do you plan to address any of the most recent comments? Either way, when you have finished, squash the commits down to one or two (smi + smi-nand) and then git push -f to update the PR.

@Wren6991

Wren6991 commented Sep 4, 2015

Copy link
Copy Markdown
Contributor Author

I've made the cosmetic changes for notro, and fixed a couple of minor issues with user space DMA. I'm limited in the testing I can do here, but SMI NAND driver works fine, and I haven't found any more issues with SMI in its current state.

I'll squash into two commits, one for SMI + char dev and one for SMI NAND.

Signed-off-by: Luke Wren <wren6991@gmail.com>
Signed-off-by: Luke Wren <wren6991@gmail.com>
@Wren6991

Wren6991 commented Sep 5, 2015

Copy link
Copy Markdown
Contributor Author

@popcornmix @pelwell I've squashed my commits. How do you feel about the driver now?

@popcornmix

Copy link
Copy Markdown
Collaborator

The defconfig changes in second commit are bcm2709_defconfig only.
Any reason not to change bcmrpi_defconfig too?

@popcornmix
popcornmix force-pushed the rpi-4.1.y branch 2 times, most recently from 03a8bad to d86a5c3 Compare September 14, 2015 18:02
@pelwell

pelwell commented Sep 16, 2015

Copy link
Copy Markdown
Contributor

I've modified bcmrpi_defconfig appropriately and pushed the resulting commits to 4.1 and 4.2.

@pelwell pelwell closed this Sep 16, 2015
popcornmix added a commit to raspberrypi/firmware that referenced this pull request Sep 17, 2015
See: raspberrypi/linux#1131

kernel: config: Add CIFS_DFS_UPCALL, CIFS_ACL, CIFS_SMB2, CIFS_FSCACHE
See: raspberrypi/linux#1133

kernel: Add SMI driver
See: raspberrypi/linux#1105

kernel: dwc_otg: Force host mode to fix incorrect compute module boards

firmware: video codec: Cleanup of fix for regression from refactor userdata release mechanics in categoriser commit
popcornmix added a commit to Hexxeh/rpi-firmware that referenced this pull request Sep 17, 2015
See: raspberrypi/linux#1131

kernel: config: Add CIFS_DFS_UPCALL, CIFS_ACL, CIFS_SMB2, CIFS_FSCACHE
See: raspberrypi/linux#1133

kernel: Add SMI driver
See: raspberrypi/linux#1105

kernel: dwc_otg: Force host mode to fix incorrect compute module boards

firmware: video codec: Cleanup of fix for regression from refactor userdata release mechanics in categoriser commit
neuschaefer pushed a commit to neuschaefer/raspi-binary-firmware that referenced this pull request Feb 27, 2017
See: raspberrypi/linux#1131

kernel: config: Add CIFS_DFS_UPCALL, CIFS_ACL, CIFS_SMB2, CIFS_FSCACHE
See: raspberrypi/linux#1133

kernel: Add SMI driver
See: raspberrypi/linux#1105

kernel: dwc_otg: Force host mode to fix incorrect compute module boards

firmware: video codec: Cleanup of fix for regression from refactor userdata release mechanics in categoriser commit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants